题解 P2834 【能力测验】
\[ans=\sum_{i=1}^n\sum_{j=1}^m(n\;mod\;i)\times(m\;mod\;j),i\neq j
\]
我们假设
\[n\leq m
\]
\[\begin{aligned}
ans
& =\sum_{i=1}^n\sum_{j=1}^m(n\;mod\;i)\times(m\;mod\;j),i\neq j
\\ & =\sum_{i=1}^{n}\sum_{j=1}^m(n\;mod\;i)\times(m\;mod\;j)-\sum_{i=1}^n(n\;mod\;i)\times(m\;mod\;i)
\\ & =\sum_{i=1}^{n}\sum_{j=1}^m(n-\left\lfloor\dfrac{n}{i}\right\rfloor\cdot i)\times(m-\left\lfloor\dfrac{m}{j}\right\rfloor\cdot j)-\sum_{i=1}^n(n-\left\lfloor\dfrac{n}{i}\right\rfloor\cdot i)\times(m-\left\lfloor\dfrac{m}{i}\right\rfloor\cdot i)
\\ & =\sum_{i=1}^{n}(n-\left\lfloor\dfrac{n}{i}\right\rfloor\cdot i)\sum_{j=1}^m(m-\left\lfloor\dfrac{m}{j}\right\rfloor\cdot j)-\sum_{i=1}^n(n-\left\lfloor\dfrac{n}{i}\right\rfloor\cdot i)\times(m-\left\lfloor\dfrac{m}{i}\right\rfloor\cdot i)
\\ & =\sum_{i=1}^{n}(n-\left\lfloor\dfrac{n}{i}\right\rfloor\cdot i)\sum_{j=1}^m(m-\left\lfloor\dfrac{m}{j}\right\rfloor\cdot j)-\sum_{i=1}^n(n\cdot m - i\cdot(\left\lfloor\dfrac{n}{i}\right\rfloor\cdot m+\left\lfloor\dfrac{m}{i}\right\rfloor\cdot n)+\left\lfloor\dfrac{n}{i}\right\rfloor\left\lfloor\dfrac{m}{i}\right\rfloor i^2)
\end{aligned}
\]
然后
前面的等式先用数论分块算出j循环的值,然后第二次数论分块分i的值,从而将前面的等式算出来。
后面的等式直接数论分块
r = min(n / (n / l), m / (m / l));
换个样子就可以分了。
然后要用平方和公式,所以得先把2,6的逆元算出来
\[\sum_{i=1}^ni^2=\frac{i\cdot(i+1)\cdot(2\cdot i +1)}{6}
\]
剩下的就没什么了
#include<cstdio>
#define Starseven main
#define ll long long
const ll mod = 19940417;
ll Add(ll l, ll r) {
ll re = 0, ny2 = 9970209;
re = (1 + r) * r % mod * ny2 % mod;
re %= mod;
l--;
re = (re - (l + 1) * l % mod * ny2 % mod) % mod;
re = (re + mod) % mod;
return re;
}
ll All(ll l, ll r) {
ll ny6 = 3323403;
ll re = 0;
re = r * (r + 1) % mod * ((2 * r + 1) % mod) % mod * ny6 % mod;
l--;
re = (re - (l * (l + 1) % mod *((2 * l + 1) % mod) % mod * ny6 % mod)) % mod;
re = (re + mod) % mod;
return re;
}
int Starseven(void) {
ll n, m;
read(n);
read(m);
if(n > m) swap(n, m);
ll ans = 0, judge = 0;
for (ll l = 1, r; l <= m; l = r + 1) {
r = m / (m / l);
ll hack = (r - l + 1) * m % mod;
hack = (hack - (m / l) * Add(l, r) % mod) % mod;
hack = (hack + mod) % mod;
judge = (judge + hack) % mod;
}
for (ll l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ll hack = (r - l + 1) * n % mod;
hack = (hack - (n / l) * Add(l, r) % mod) % mod;
hack = (hack + mod) % mod;
hack = hack * judge % mod;
ans = (ans + hack) % mod;
}
for (ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ll hack = n * m % mod * (r - l + 1) % mod;
hack += (n / l) * (m / l) % mod * All(l, r) % mod;
hack %= mod;
ll a = m * (n / l) % mod, b = n * (m / l) % mod;
hack = (hack - Add(l, r) * (a + b) % mod) % mod;
hack = (hack + mod) % mod;
ans = (ans - hack) % mod;
ans = (ans + mod) % mod;
}
write(ans);
puts("");
return 0;
}