P2260 [清华集训2012]模积和
推导过程
我们假定n<=mn <= mn<=m
∑i=1n∑j=1m(nmodi)(mmodj),i≠j\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} (n\mod i)(m \mod j), i \not= ji=1∑nj=1∑m(nmodi)(mmodj),i=j
=∑i=1n∑j=1m(nmodi)(mmodj)−∑k=1n(nmodk)(mmodk)= \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} (n\mod i)(m \mod j) - \sum_{k = 1} ^{n} (n \mod k) (m \mod k)=i=1∑nj=1∑m(nmodi)(mmodj)−k=1∑n(nmodk)(mmodk)
=∑i=1n(n−⌊ni⌋i)∑j=1m(m−⌊mj⌋j)−∑k=1n(n−⌊nk⌋k)(m−⌊mk⌋k)= \sum_{i = 1} ^{n} (n - \lfloor \frac{n}{i}\rfloor i) \sum_{j = 1} ^{m} (m - \lfloor \frac{m}{j}\rfloor j) - \sum_{k = 1} ^{n} (n - \lfloor \frac{n}{k}\rfloor k) (m - \lfloor \frac{m}{k}\rfloor k)=i=1∑n(n−⌊in⌋i)j=1∑m(m−⌊jm⌋j)−k=1∑n(n−⌊kn⌋k)(m−⌊km⌋k)
=(n2−∑i=1n⌊ni⌋i)(m2−∑j=1m⌊mj⌋j)−∑k=1n(nm−nk⌊mk⌋−mk⌊nk⌋)+k2⌊nkmk⌋= (n ^ 2 - \sum_{i = 1} ^{n} \lfloor \frac{n}{i}\rfloor i)(m ^ 2 - \sum_{j = 1} ^{m} \lfloor \frac{m}{j}\rfloor j) - \sum_{k = 1} ^{n}(nm - nk \lfloor{\frac{m}{k}}\rfloor - mk \lfloor{\frac{n}{k}}\rfloor) + k ^ 2 \lfloor{\frac{n}{k}}\frac{m}{k}\rfloor=(n2−i=1∑n⌊in⌋i)(m2−j=1∑m⌊jm⌋j)−k=1∑n(nm−nk⌊km⌋−mk⌊kn⌋)+k2⌊knkm⌋
之后我们可以利用整除分块加平方和公式与逆元结合求得最后答案
1+22+32+……+n2=n(n+1)(2n+1)61 + 2 ^ 2 + 3 ^ 2 + …… + n ^ 2 = \frac{n(n + 1)(2n + 1)}{6}1+22+32+……+n2=6n(n+1)(2n+1)
多余的化简,只要上面的就行了,一开始我化简成了下面的式子,以为更简单,结果求崩了,找不出bugbugbug,,,
=(n2−∑i=1n⌊ni⌋i)(m2−∑j=1m⌊mj⌋j)−n2m+∑k=1n(nk⌊mk⌋+mk⌊nk⌋)−k2⌊nk⌋⌊mk⌋= (n ^ 2 - \sum_{i = 1} ^{n} \lfloor \frac{n}{i}\rfloor i)(m ^ 2 - \sum_{j = 1} ^{m} \lfloor \frac{m}{j}\rfloor j) - n ^ 2m + \sum_{k = 1} ^{n}(nk \lfloor{\frac{m}{k}}\rfloor + mk \lfloor{\frac{n}{k}}\rfloor) - k ^ 2 \lfloor{\frac{n}{k}}\rfloor\lfloor\frac{m}{k}\rfloor=(n2−i=1∑n⌊in⌋i)(m2−j=1∑m⌊jm⌋j)−n2m+k=1∑n(nk⌊km⌋+mk⌊kn⌋)−k2⌊kn⌋⌊km⌋
=(n2−∑i=1n⌊ni⌋i)(m2−∑j=1m⌊mj⌋j)−n2m+n2∑k=1nk⌊mk⌋+nm∑k=1nk⌊nk⌋)−∑k=1nk2⌊nk⌋⌊mk⌋= (n ^ 2 - \sum_{i = 1} ^{n} \lfloor \frac{n}{i}\rfloor i)(m ^ 2 - \sum_{j = 1} ^{m} \lfloor \frac{m}{j}\rfloor j) - n ^ 2m + n ^ 2\sum_{k = 1} ^{n}k \lfloor{\frac{m}{k}}\rfloor + nm\sum_{k = 1} ^{n}k \lfloor{\frac{n}{k}}\rfloor) - \sum_{k = 1} ^{n}k ^ 2 \lfloor{\frac{n}{k}}\rfloor\lfloor\frac{m}{k}\rfloor=(n2−i=1∑n⌊in⌋i)(m2−j=1∑m⌊jm⌋j)−n2m+n2k=1∑nk⌊km⌋+nmk=1∑nk⌊kn⌋)−k=1∑nk2⌊kn⌋⌊km⌋
代码
注意随手取模,容易溢出wawawa。
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll x = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return x * f;
}const int mod = 19940417, inv = 3323403;ll calc1(ll n) {return n * (n + 1) % mod * (2 * n + 1) % mod * inv % mod;
}ll calc2(ll l, ll r) {return (l + r) * (r - l + 1) / 2 % mod;
}ll f(ll n) {ll ans = 0;for(ll l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans + n * (r - l + 1) % mod - calc2(l, r) * (n / l) % mod + mod) % mod;}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);ll n = read(), m = read();if(n > m) swap(n, m);ll ans = (f(n) * f(m)) % mod;for(ll l = 1, r; l <= n; l = r + 1) {r = min(n / (n / l), m / (m / l));ll temp1 = n * m % mod * (r - l + 1) % mod;ll temp2 = n * calc2(l, r) % mod * (m / l) % mod;ll temp3 = m * calc2(l, r) % mod * (n / l) % mod;ll temp4 = (calc1(r) - calc1(l - 1) + mod) * (n / l) % mod * (m / l) % mod;ans = (ans - (temp1 - temp2 - temp3 + temp4) % mod + mod) % mod;}cout << ans << endl;return 0;
}
P2834 能力测验
照搬上面的代码,改一下模数和逆元即可。
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll x = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return x * f;
}const int mod = 1000000007, inv = 166666668;ll calc1(ll n) {return n * (n + 1) % mod * (2 * n + 1) % mod * inv % mod;
}ll calc2(ll l, ll r) {return (l + r) * (r - l + 1) / 2 % mod;
}ll f(ll n) {ll ans = 0;for(ll l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans + n * (r - l + 1) % mod - calc2(l, r) * (n / l) % mod + mod) % mod;}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);ll n = read(), m = read();if(n > m) swap(n, m);ll ans = (f(n) * f(m)) % mod;for(ll l = 1, r; l <= n; l = r + 1) {r = min(n / (n / l), m / (m / l));ll temp1 = n * m % mod * (r - l + 1) % mod;ll temp2 = n * calc2(l, r) % mod * (m / l) % mod;ll temp3 = m * calc2(l, r) % mod * (n / l) % mod;ll temp4 = (calc1(r) - calc1(l - 1) + mod) * (n / l) % mod * (m / l) % mod;ans = (ans - (temp1 - temp2 - temp3 + temp4) % mod + mod) % mod;}cout << ans << endl;return 0;
}