Blog of RuSun

OneProblemIsDifficultBecauseYouDontKnowWhyItIsDiffucult

P2261 [CQOI2007]余数求和

P2261 [CQOI2007]余数求和

答案为
G(n,k)=i=1nkmodi=i=1nkiki=i=1nki=1niki=nki=1niki
直接数论分块即可。需要注意的是,这里 nk ,所以 r = min(n, k / (k / l)); 而不是 r = k / (k / l);

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL n, k;
int main()
{
scanf("%lld%lld", &n, &k);
LL res = n * k;
for (int l = 1, r; l <= min(n, k); l = r + 1)
{
r = min(n, k / (k / l));
res -= (k / l) * (l + r) * (r - l + 1) / 2;
}
printf("%lld", res);
return 0;
}

Gitalk 加载中 ...