bzoj 2956 模积和

整除分块.

可以把 $i=j$ 的贡献算上,后面再减掉.

假设 $n\le m$ .

$$
\begin{aligned}
ans&=\sum_{i=1}^n \sum_{j=1}^m (n\bmod i)(m\bmod j)-\sum_{i=1}^n (n\bmod i)(m\bmod i) \\
&=\sum_{i=1}^n \sum_{j=1}^m (n-\lfloor\frac n i \rfloor\cdot i)(m-\lfloor\frac m j \rfloor\cdot j)-\sum_{i=1}^n (n-\lfloor\frac n i \rfloor\cdot i)(m-\lfloor\frac m i \rfloor\cdot i) \\
&=\sum_{i=1}^n (n-\lfloor\frac n i \rfloor\cdot i)\sum_{i=1}^m (m-\lfloor\frac m i \rfloor\cdot i) -\sum_{i=1}^n nm+i^2\cdot \lfloor\frac n i \rfloor\lfloor\frac m i \rfloor-n\cdot \lfloor\frac m i \rfloor\cdot i-m\cdot \lfloor\frac n i \rfloor \cdot i
\end{aligned}
$$
整除分块计算即可.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int P=19940417;
const int inv2=(P+1)>>1,inv6=3323403;
int add(int a,int b)
{
return (a+b>=P)?(a+b-P):(a+b);
}
void upd(int &x,int y)
{
x=add(x,y);
}
int mul(int a,int b)
{
return 1LL * a * b % P;
}
int S1(int l,int r)
{
return mul(mul(l+r,r-l+1),inv2);
}
int s2(int x)
{
return mul(mul(x,mul(x+1,2*x+1)),inv6);
}
int S2(int l,int r)
{
return add(s2(r),P-s2(l-1));
}
int calc(int n)
{
int ans=0;
for(int l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
upd(ans,mul(n,r-l+1));
upd(ans,P-mul(S1(l,r),n/l));
}
return ans;
}
int main()
{
int n=read(),m=read();
if(n>m)
swap(n,m);
int ans=0;
upd(ans,mul(calc(n),calc(m)));
for(int l=1,r;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
upd(ans,P-mul(mul(n,m),r-l+1));
upd(ans,P-mul(mul(n/l,m/l),S2(l,r)));
upd(ans,mul(mul(n,m/l),S1(l,r)));
upd(ans,mul(mul(m,n/l),S1(l,r)));
}
cout<<ans<<endl;
return 0;
}