莫比乌斯反演套路三、四--BZOJ2154: Crash的数字表格 && BZOJ2693: jzptab

t<=1e4个询问每次问n,m<=1e7，$\sum_{1\leqslant x \leqslant n,1 \leqslant y\leqslant m}lcm(x,y)$。

 1 //#include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 #include<cstdio>
5 //#include<bitset>
6 #include<algorithm>
7 //#include<cmath>
8 using namespace std;
9
10 const int mod=20101009;
11 int T,n,m;
12 #define maxn 10000011
13 int inv[maxn],miu[maxn],prime[maxn],sum[maxn],lp; bool notprime[maxn];
14 void pre(int n)
15 {
16     miu[1]=1; lp=0; sum[1]=1;
17     long long tmp;
18     for (int i=2;i<=n;i++)
19     {
20         if (!notprime[i]) {prime[++lp]=i; miu[i]=-1; sum[i]=mod-i+1;}
21         for (int j=1;j<=lp && (tmp=1ll*prime[j]*i)<=n;j++)
22         {
23             notprime[tmp]=1;
24             if (i%prime[j]) miu[tmp]=-miu[i],sum[tmp]=1ll*sum[i]*sum[prime[j]]%mod;
25             else {miu[tmp]=0; sum[tmp]=sum[i]; break;}
26         }
27     }
28     for (int i=2;i<=n;i++) sum[i]=1ll*sum[i]*i%mod,sum[i]+=sum[i-1],sum[i]-=sum[i]>=mod?mod:0;
29 //    for (int i=1;i<=n;i++) cout<<sum[i]<<' ';cout<<endl;
30 }
31
32 int main()
33 {
34     scanf("%d%d",&n,&m);
35     pre(min(n,m));
36 //    scanf("%d",&T);
37 //while (T--)
38 //{
39     int ans=0;
40     for (int i=1,to=min(n,m),last,hh=((mod+1)>>1)*1ll*((mod+1)>>1)%mod;i<=to;i=last+1)
41     {
42         last=min(n/(n/i),m/(m/i));
43         ans+=1ll*(n/i)*(m/i)%mod*(1+(n/i))%mod*(1+(m/i))%mod*hh%mod*(sum[last]-sum[i-1])%mod;
44         ans-=ans>=mod?mod:0,ans+=ans<0?mod:0;
45     }
46     printf("%d\n",ans);
47 //}
48     return 0;
49 }
View Code

posted @ 2018-01-04 07:24  Blue233333  阅读(134)  评论(0编辑  收藏  举报