标题
增加 1 行删除 1 行
1题解:P2260 [清华集训2012]模积和1题解:P2260 [清华集训2012] 模积和
正文
增加 7 行删除 91 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697原式就等于:$$\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)$$```cpp#include<bits/stdc++.h>using 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;}```12345678910111213不妨设$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{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$坨数论分块就可以了