# 杜教筛小结

……咳咳，言归正传，现在来总结一下杜教筛……

https://www.cnblogs.com/abclzr/p/6242020.html

1.bzoj3944

2.bzoj4176

$d\mid ij\Leftrightarrow \frac{d}{(i,d)}\mid j$

……自己打公式太麻烦了，放弃……

……其实怎么演都能演出来，关键就看第一步转化能不能出来

 1 #include <cstdio>
2 #include <map>
3 #include <cstring>
4 using namespace std;
5 #define mod 1000000007
6 #define N 1000000
7 #define LL long long
8 int n,prime[N/10],tot,vis[N+10],mu[N+10];
9 inline void intn()
10 {
11     register int i,j,tmp;
12     for(mu[1]=1,i=2;i<=N;++i)
13     {
14         if(!vis[i])prime[++tot]=i,mu[i]=-1;
15         for(j=1;j<=tot&&(tmp=i*prime[j])<=N;++j)
16             {vis[tmp]=1;if(i%prime[j]==0){mu[tmp]=0;break;}mu[tmp]=-mu[i];}
17     }
18     for(i=1;i<=N;++i)mu[i]+=mu[i-1];
19 }
20 map<int,int> mmp;
21 inline int getmu(int x)
22 {
23     if(x<=N)return mu[x];
24     if(mmp.count(x))return mmp[x];
25     register int i,last,ret=1;
26     for(i=2;i<=x;i=last+1)
27         last=x/(x/i),ret=(ret-((LL)(last-i+1)*getmu(x/i)%mod))%mod;
28     return mmp[x]=ret;
29 }
30 inline int calc2(int x)
31 {
32     register int i,last,ret=0;
33     for(i=1;i<=x;i=last+1)
34         last=x/(x/i),ret=(ret+((LL)(last-i+1)*(x/i)%mod))%mod;
35     return (LL)ret*ret%mod;
36 }
37 int main()
38 {
39     // freopen("Ark.in","r",stdin);
40     register int i,last,ans=0;
41     scanf("%d",&n);intn();
42     for(i=1;i<=n;i=last+1)
43         last=n/(n/i),ans=(ans+((LL)(getmu(last)-getmu(i-1))*calc2(n/i)%mod) )%mod;
44     printf("%d\n",(ans+mod)%mod);
45 }
bzoj4176

3.bzoj3512

$S(n,m)=\varphi(p_{i}) * \sum _{i=1} ^{m} \varphi(\frac{n}{p_{i}}*i)+\sum _{i=1} ^{ \left \lfloor \frac{m}{p_{i}} \right \rfloor } \varphi(n*i)$

$S(n,m)=S(\frac{n}{p_{i}},m)+S(n, \left \lfloor \frac{m}{p_{i}} \right \rfloor)$

 1 #include <cstdio>
2 #include <cstring>
3 #include <map>
4 using namespace std;
5 #define N 1000000
6 #define LL long long
7 #define mod 1000000007
8 int prime[N/10],tot,mind[N+10],phi[N+10],sum[N+10];
9 inline void intn()
10 {
11     register int i,j,tmp;
12     phi[1]=1;
13     for(i=2;i<=N;++i)
14     {
15         if(!mind[i])prime[++tot]=i,mind[i]=i,phi[i]=i-1;
16         for(j=1;j<=tot&&(tmp=i*prime[j])<=N;++j)
17         {
18             mind[tmp]=prime[j];
19             if(i%prime[j]==0){phi[tmp]=phi[i]*prime[j];break;}
20             phi[tmp]=phi[i]*phi[prime[j]];
21         }
22     }
23     for(i=1;i<=N;++i)sum[i]=(sum[i-1]+phi[i])%mod;
24 }
25 inline int quick_mod(int di,int mi)
26 {
27     int ans=1;
28     for(di%=mod;mi;mi>>=1,di=(LL)di*di%mod)
29         if(mi&1)ans=(LL)ans*di%mod;
30     return ans;
31 }
32 inline int divide(int x)
33 {
34     register int ret=1,i;
35     for(i=1;(LL)prime[i]*prime[i]<=x;++i)
36         if(x%prime[i]==0)
37         {
38             ret*=prime[i];
39             while(x%prime[i]==0)x/=prime[i];
40         }
41     return ret*x;
42 }
43 #define N1 100010
44 map<int,int>mmp;
45 int mem[N1];
46 inline int getphi(int x)
47 {
48     if(x<=N)return sum[x];
49     else if(mmp.count(x))return mmp[x];
50     int ret=((LL)x*(x+1)/2%mod);
51     register int i,last;
52     for(i=2;i<=x;i=last+1)
53         last=x/(x/i),ret=( ret+mod-( (LL)(last-i+1)*getphi(x/i)%mod ) )%mod;
54     return mmp[x]=ret;
55 }
56 inline int calc(int a,int b)
57 {
58     if(!b)return 0;
59     if(b==1)return phi[a];
60     if(a==1)return getphi(b);
61     return ((LL)calc(a/mind[a],b)*(mind[a]-1)%mod+calc(a,b/mind[a]))%mod;
62 }
63 int main()
64 {
65     // freopen("Ark.in","r",stdin);
66     register int n,m,ans=0,tmp,x,i;
67     scanf("%d%d",&n,&m),intn();
68     memset(mem,-1,sizeof(mem));
69     for(i=1;i<=n;++i)
70     {
71         x=divide(i),tmp=i/x;
72         if(mem[x]==-1)mem[x]=calc(x,m);
73         ans=(ans+(LL)tmp*mem[x]%mod)%mod;
74     }
75     printf("%d\n",ans);
76 }
bzoj3512

4.bzoj4916

首先你发现$\sum \mu(i^{2})$是出来搞笑的，输出1即可

$\varphi(i)=\prod \varphi(p_{i})*p_{i} ^{a_{i}-1}$

$\varphi(i^{2})=\prod \varphi(p_{i})*p_{i} ^{a_{i}-1+a_{i}}$

$\varphi(i^{2})=\prod \varphi(p_{i})*p_{i} ^{a_{i}-1} *p_{i}^{a_{i}}$

 1 #include <cstdio>
2 #include <map>
3 #include <cstring>
4 using namespace std;
5 #define N 1000000
6 #define mod 1000000007
7 int n,inv6,vis[N+10],prime[N/10],tot,phi[N+10],sum[N+10];
8 map<int,int> mmp;
9 #define LL long long
10 inline void intn()
11 {
12     register int i,j,tmp;
13     for(phi[1]=1,i=2;i<=N;++i)
14     {
15         if(!vis[i])prime[++tot]=i,phi[i]=i-1;
16         for(j=1;j<=tot&&(tmp=i*prime[j])<=N;++j)
17         {
18             if(i%prime[j]==0)
19                 {vis[tmp]=1,phi[tmp]=phi[i]*prime[j];break;}
20             vis[tmp]=1,phi[tmp]=phi[i]*phi[prime[j]];
21         }
22     }
23     for(i=1;i<=N;++i)
24         sum[i]=(sum[i-1]+(LL)i*phi[i]%mod)%mod;
25 }
26 inline int quick_mod(int di,int mi)
27 {
28     int ans=1;
29     for(;mi;mi>>=1,di=(LL)di*di%mod)
30         if(mi&1)ans=(LL)ans*di%mod;
31     return ans;
32 }
33 inline int getf(int x)
34 {
35     if(x<=N)return sum[x];
36     if(mmp.count(x))return mmp[x];
37     register int i,last,ret=(LL)x*(x+1)%mod*(2*x+1)%mod*inv6%mod;
38     for(i=2;i<=x;i=last+1)
39         last=x/(x/i),
40           ret=(   ret +mod -(LL)getf(x/i)*( (LL)(last-i+1)*(last+i)/2 %mod )%mod   )%mod;
41     return mmp[x]=ret;
42 }
43 int main()
44 {
45     // freopen("Ark.in","r",stdin);
46     inv6=quick_mod(6,mod-2);
47     scanf("%d",&n),intn();
48     printf("1\n%d\n",getf(n));
49 }
bzoj4916

5.bzoj3930

$f(n)=\sum _{n|d} \mu(\frac{d}{n})*F(d)$

$f(n)=\sum_{i=1}^{ \left \lfloor \frac{r}{n} \right \rfloor } \mu(i) * ( \left \lfloor \frac{r}{i*n} \right \rfloor - \left \lfloor \frac{l-1}{i*n} \right \rfloor )^{n}$

 1 #include <cstdio>
2 #include <map>
3 #include <cstring>
4 using namespace std;
5 #define mod 1000000007
6 #define LL long long
7 #define inf 0x3f3f3f3f
8 #define N 1000000
9 int prime[N],tot,mu[N+10],vis[N+10];
10 int n,d,l,r;
11 inline void intn()
12 {
13     register int i,j,tmp;
14     for(mu[1]=1,i=2;i<=N;++i)
15     {
16         if(!vis[i])prime[++tot]=i,mu[i]=-1;
17         for(j=1;j<=tot&&(tmp=i*prime[j])<=N;++j)
18         {
19             vis[tmp]=1;
20             if(i%prime[j]==0)break;
21             mu[tmp]=-mu[i];
22         }
23     }
24     for(i=2;i<=N;++i)mu[i]+=mu[i-1];
25 }
26 inline int min(int a,int b){return a<b?a:b;}
27 map<int,int> mmp;
28 inline LL getmu(int x)
29 {
30     if(x<=N)return mu[x];
31     if(mmp.count(x))return mmp[x];
32     LL ret=1;
33     for(register int i=2,last;i<=x;i=last+1)
34         last=x/(x/i),ret=( ret-getmu(x/i)*(LL)(last-i+1)%mod )%mod;
35     return mmp[x]=ret;
36 }
37 inline LL quick_mod(LL di,int mi)
38 {
39     LL ans=1;
40     for(;mi;mi>>=1,di=di*di%mod)
41         if(mi&1)ans=ans*di%mod;
42     return ans;
43 }
44 signed main()
45 {
46     register int i,last;
47     scanf("%d%d%d%d",&n,&d,&l,&r);
48     r=r/d,l=(l-1)/d,intn();
49     LL ans=0;
50     for(i=1;i<=r;i=last+1)
51     {
52         last= (l/i) ? min( r/(r/i), l/(l/i) ) : r/(r/i) ,
53         ans=(ans+ ( getmu(last)-getmu(i-1) )*quick_mod((r/i-l/i),n) )%mod;
54     }
55     printf("%lld\n",(ans%mod+mod)%mod);
56 }
bzoj3930

6.bzoj4652

…………不得不说是个好题

$\sum _{i=1} ^{n} \sum _{j=1} ^{m} f(j)[(i,j)==1]$

$\sum _{i=1} ^{n} \sum _{j=1} ^{m} f(j) \sum _{d|(i,j)}\mu(d)$

$\sum _{d=1} ^{n} \mu(d)\left \lfloor \frac{n}{d} \right \rfloor \sum _{j=1}^{ \left \lfloor \frac{m}{d} \right \rfloor }f(j*d)$

......然后不会处理$\sum _{j=1}^{ \left \lfloor \frac{m}{d} \right \rfloor }f(j)$这一部分了……我以为可以杜教筛这部分，结果发现不行

$f(i)\Leftrightarrow[(i,k)==1]$

……当时整个人傻了

$F(n)=\left \lfloor \frac{n}{k} \right \rfloor F(k)+F(n \% k)$

$\sum _{d=1} ^{n} \mu(d) [(k,d)==1] \left \lfloor \frac{n}{d} \right \rfloor F( \left \lfloor \frac{m}{d} \right \rfloor )$

$G(n,k)=G(n,q)-\sum _{y=1} ^{\left \lfloor \frac{n}{p} \right \rfloor } \mu(py) [(y,q)==1] [(y,p)==1]$

$k==1$时$G(n,1)=\sum _{i=1} ^{n} \mu(i)$，杜教筛之。

 1 #include <map>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define N 20000000
6 #define K 2010
7 #define LL long long
8 int n,m,k;
9 int prime[N/10+10],tot,mu[N+10],sum[K];
10 bool vis[N+10];
11 inline int min(int a,int b){return a<b?a:b;}
12 inline int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
13 inline void init()
14 {
15     register int i,j,tmp;
16     for(i=1;i<=k;++i)sum[i]=sum[i-1]+(gcd(i,k)==1);
17     for(mu[1]=1,i=2;i<=N;++i)
18     {
19         if(!vis[i])prime[++tot]=i,mu[i]=-1;
20         for(j=1;j<=tot&&(tmp=i*prime[j])<=N;++j)
21         {
22             vis[tmp]=1;
23             if(i%prime[j]==0){mu[tmp]=0;break;}
24             mu[tmp]=-mu[i];
25         }
26     }
27     for(i=1;i<=N;++i)mu[i]+=mu[i-1];
28 }
29 int bin[100],ge;
30 inline void divide(int kkk)
31 {
32     register int i;
33     for(i=1;prime[i]*prime[i]<=kkk;++i)
34         if(kkk%prime[i]==0)
35         {
36             bin[++ge]=prime[i];
37             while(kkk%prime[i]==0)kkk/=prime[i];
38         }
39     if(kkk>1)bin[++ge]=kkk;
40 }
41 inline int getF(int x)
42 {
43     if(x<=k)return sum[x];
44     return (x/k)*sum[k]+sum[x%k];
45 }
46 map<int,int>smu;
47 inline int getmu(int x)
48 {
49     if(x<=N)return mu[x];
50     if(smu.count(x))return smu[x];
51     register int i,last,ret=1;
52     for(i=2;i<=x;i=last+1)
53         last=x/(x/i),ret-=(LL)getmu(x/i)*(last-i+1);
54     return smu[x]=ret;
55 }
56 map<int,int>mmp[7];
57 inline int getsum(int x,int layer)
58 {
59     if(x<=1)return x;
60     if(layer==0)return getmu(x);
61     if(mmp[layer].count(x))return mmp[layer][x];
62     return mmp[layer][x]=getsum(x,layer-1)+getsum(x/bin[layer],layer);
63 }
64 int main()
65 {
66     scanf("%d%d%d",&n,&m,&k);
67     init(),divide(k);
68     register int i,to=min(n,m),last;
69     LL ans=0;
70     // if(n>m)n^=m,m^=n,n^=m;
71     for(i=1;i<=to;i=last+1)
72         last=min(n/(n/i),m/(m/i)),
73         ans+=(getsum(last,ge)-getsum(i-1,ge))*(LL)getF(m/i)*(n/i);
74     printf("%lld\n",ans);
75 }
bzoj4652

posted @ 2018-01-22 19:01 LadyLex 阅读(...) 评论(...) 编辑 收藏