[Problem]
给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数。例如j(5, 3)=3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5=0+1+0+3+3=7
[Solution]
It's now not easy to find such an easy problem on BZOJ.
Consider k % i = k - i * [k / i] => ans = k * n - sigma(i * [k / i]).
[k / i] is something just like we do using Function Mobius. The optimization can make the time complex sqrt(k).
By the way, when i >= k, [k / i] = 0. At that time, you should break or the program can get crashed.
Then it's very easy to code.
[Code]
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long qw;
#ifdef WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
qw k, n, s;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
scanf(lld lld, &n, &k);
s = n * k;
for (qw i = 1, j; i <= n; i = j + 1)
if (k / i == 0)
break;
else {
j = min(n, k / (k / i));
s -= (j - i + 1) * (j + i) * (k / i) / 2;
}
printf(lld "\n", s);
}