我们假定 n < = m n <= m n<=m
∑ i = 1 n ∑ j = 1 m ( n m o d i ) ( m m o d j ) , i ≠ j \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} (n\mod i)(m \mod j), i \not= j i=1∑nj=1∑m(nmodi)(mmodj),i=j
= ∑ i = 1 n ∑ j = 1 m ( n m o d i ) ( m m o d j ) − ∑ k = 1 n ( n m o d k ) ( m m o d k ) = \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 = 1 n ( n − ⌊ n i ⌋ i ) ∑ j = 1 m ( m − ⌊ m j ⌋ j ) − ∑ k = 1 n ( n − ⌊ n k ⌋ k ) ( m − ⌊ m k ⌋ 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)
= ( n 2 − ∑ i = 1 n ⌊ n i ⌋ i ) ( m 2 − ∑ j = 1 m ⌊ m j ⌋ j ) − ∑ k = 1 n ( n m − n k ⌊ m k ⌋ − m k ⌊ n k ⌋ ) + k 2 ⌊ n k m k ⌋ = (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 + 2 2 + 3 2 + … … + n 2 = n ( n + 1 ) ( 2 n + 1 ) 6 1 + 2 ^ 2 + 3 ^ 2 + …… + n ^ 2 = \frac{n(n + 1)(2n + 1)}{6} 1+22+32+……+n2=6n(n+1)(2n+1)
多余的化简,只要上面的就行了,一开始我化简成了下面的式子,以为更简单,结果求崩了,找不出 b u g bug bug,,,
= ( n 2 − ∑ i = 1 n ⌊ n i ⌋ i ) ( m 2 − ∑ j = 1 m ⌊ m j ⌋ j ) − n 2 m + ∑ k = 1 n ( n k ⌊ m k ⌋ + m k ⌊ n k ⌋ ) − k 2 ⌊ n k ⌋ ⌊ m k ⌋ = (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⌋
= ( n 2 − ∑ i = 1 n ⌊ n i ⌋ i ) ( m 2 − ∑ j = 1 m ⌊ m j ⌋ j ) − n 2 m + n 2 ∑ k = 1 n k ⌊ m k ⌋ + n m ∑ k = 1 n k ⌊ n k ⌋ ) − ∑ k = 1 n k 2 ⌊ n k ⌋ ⌊ m k ⌋ = (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⌋
注意随手取模,容易溢出 w a wa wa。
/*
Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include
#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 | 1
using 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;
}
照搬上面的代码,改一下模数和逆元即可。
/*
Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include
#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 | 1
using 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;
}