版本比较
题解:P2260 [清华集训2012] 模积和
标题
增加 1 行删除 1 行
11题解:P2260 [清华集训2012]模积和题解:P2260 [清华集训2012] 模积和正文
增加 2 行删除 72 行
1234123456710111289101112131920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283原式就等于:
$$ \sum_{i = 1} ^ n \sum_{j = 1} ^ m (n \bmod i) \times (m \bmod j),i \not=j $$
不妨设$n \leq m$。
那原式:
$$ = \sum_{i = 1} ^ n (n \bmod i) \times \sum_{j = 1} ^ m (m \bmod j) - \sum_{i = 1} ^ n (n \bmod i) \times (m \bmod i) $$
$$ = \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i)\times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j) - \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) (m - \left \lfloor \frac{n}{j} \right \rfloor \times j) $$
$$ = \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i)\times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j)-\sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) (m - \left \lfloor \frac{m}{i} \right \rfloor \times i)$$
$$= \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i)\times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j)-\sum_{i = 1} ^ n (nm - n \times i \times \left \lfloor \frac{m}{i} \right \rfloor - m \times i \times \left \lfloor \frac{n}{i} \right \rfloor + i ^ 2 \times \left \lfloor \frac{n}{i} \right \rfloor \times \left \lfloor \frac{m}{i} \right \rfloor)$$
显然对这$3$坨数论分块就可以了
```cpp#include<bits/stdc++.h>namespace std;#define int long longconst int mod = 19940417;int fastpow(int a, int b){ int res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res;}int calc(int x){ return (x * (x + 1) / 2) % mod;}int calcc(int x){ return (((x * (x + 1)) % mod * (2 * x + 1)) % mod * 3323403) % mod;}signed main(){ int n, m; cin >> n >> m; if (n > m) { swap(n, m); } int ans1 = 0; for (int l = 1; l <= n; ) { int r = n / (n / l); int d = n / l; ans1 = (ans1 + mod + ((calc(r) - calc(l - 1)) * d) % mod) % mod; l = r + 1; } ans1 = (n * n % mod + mod - ans1) % mod; int ans2 = 0; for (int l = 1; l <= m; ) { int r = m / (m / l); int d = m / l; ans2 = (ans2 + mod + ((calc(r) - calc(l - 1)) * d) % mod) % mod; l = r + 1; } ans2 = (m * m % mod + mod - ans2) % mod; int ans = ans1 * ans2 % mod; int ans3 = 0; for (int l = 1; l <= n; ) { int r = min(n / (n / l), m / (m / l)); int d1 = n / l; int d2 = m / l; int b = ((calc(r) - calc(l - 1) + mod) % mod * (d1 * m % mod + d2 * n % mod)) % mod; int c = (((calcc(r) - calcc(l - 1) + mod) % mod * d1) % mod * d2) % mod; ans3 = (ans3 + (((r - l + 1) * n) % mod * m) % mod + mod - b + c) % mod; l = r + 1; } ans = (ans - ans3 + mod) % mod; cout << ans; return 0;}```