Tree IV
class Solution {
public:
typedef long long ll;
const ll mod = 998244353;
inline ll qpow(ll a, ll b) {
ll ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod, b >>= 1;
}
return ret % mod;
}
long long tree4(long long n) {
ll ans = 0, inv2 = qpow(2, mod - 2), l, r, dep;
for (l = 1, r = 1, dep = 1; r <= n; ++dep) {
ans = (ans + dep * ((l + r) % mod * (r - l + 1) % mod * inv2 % mod) % mod) % mod;
l = r + 1, r = (r + 1) * 2 - 1;
}
ans = (ans + dep * ((l + n) % mod * (n - l + 1) % mod * inv2 % mod) % mod) % mod;
return ans;
}
};
牛牛组数
bool cmp(char a,char b)
{
return a>b;
}
class Solution {
public:
/**
* 返回最大和的字符串
* @param x string字符串 即题目描述中所给字符串
* @param k int整型 即题目描述中所给的k
* @return string字符串
*/
string intToString(int num) {
stringstream ss;
ss<<num;
return ss.str();
}
string add(string s1, string s2){
string ans;
int len1 = s1.length() - 1;
int len2 = s2.length() - 1;
int s = 0;
int re = 0;
while (len1 >= 0 || len2 >= 0)
{
s = re;
if (len1 >= 0)
s += s1[len1] - '0';
if (len2 >= 0)
s += s2[len2] - '0';
re = s / 10;
ans.push_back(s % 10 + '0');
len1--;
len2--;
}
if (re)
ans.push_back(re + '0');
reverse(ans.begin(), ans.end());
return ans;
}
string Maxsumforknumers(string x, int k) {
// write code here
int n=x.size();
sort(x.begin(),x.end(),cmp);
int sum=0;
int m=n-k+1;
string str=x.substr(0,m);
for(int i=m;i<n;i++)sum+=x[i]-'0';
string str2=intToString(sum);
str=add(str,str2);
return str;
}
};
牛牛算题
long long cowModCount(long long num) {
long long ans = 0;
long long MOD = 1000000007;
for (long long i = 1, r; i <= num; i = r + 1) {
if (num / i != 0) {
r = min(num / (num / i), num);
}
else {
r = num;
}
ans += ((r - i + 1) * num * (num / i) - (num / i) * (num / i) * (r - i + 1) * (i + r) / 2) % MOD;
ans %= MOD;
}
return ans;
}
};