莫比乌斯反演
study from:
http://www.cnblogs.com/peng-ym/p/8647856.html
莫比乌斯
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <set> 7 #include <map> 8 #include <list> 9 #include <queue> 10 #include <bitset> 11 #include <vector> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 16 const int maxn=1e5+10; 17 int mu[maxn],prime[maxn],g=0,n=maxn-1; 18 bool vis[maxn]; 19 20 void mobius() 21 { 22 int i,j,k; 23 memset(mu,0,sizeof(mu)); 24 memset(vis,0,sizeof(vis)); 25 mu[1]=1; 26 for (i=2;i<=n;i++) 27 { 28 if (!vis[i]) 29 { 30 prime[++g]=i; 31 mu[i]=-1; 32 } 33 for (j=1;j<=g;j++) 34 { 35 k=i*prime[j]; 36 if (k>n) 37 break; 38 vis[k]=1; 39 if (i%prime[j]==0) 40 break; 41 mu[k]=-mu[i]; 42 } 43 } 44 } 45 46 int main() 47 { 48 mobius(); 49 int i; 50 for (i=1;i<=50;i++) 51 printf("%d ",mu[i]); 52 return 0; 53 }
整数分块
http://www.cnblogs.com/peng-ym/p/8661118.html
求和符号变换学习
https://blog.csdn.net/animalcoder/article/details/82157081
http://www.cnblogs.com/peng-ym/p/8647856.html
博客里的练习题
[SDOI2015]约数个数和
洛谷P1829 [国家集训队]Crash的数字表格
里的gcd(x,y)化简值得一看!!!
luogu2257
luogu3455
几乎同样的背景内容,由于求解不同的内容,不同的解题方法
luogu2257
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <set> 7 #include <map> 8 #include <list> 9 #include <queue> 10 #include <bitset> 11 #include <vector> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 16 #define ll long long 17 const int maxn=1e7+10; 18 const int n=1e7; 19 20 int mu[maxn],prime[maxn],g=0; 21 ll v[maxn],sum[maxn]; 22 bool vis[maxn]; 23 24 void mobius() 25 { 26 int i,j,k; 27 mu[1]=1; 28 for (i=2;i<=n;i++) 29 { 30 if (!vis[i]) 31 { 32 mu[i]=-1; 33 prime[++g]=i; 34 } 35 for (j=1;j<=g;j++) 36 { 37 k=i*prime[j]; 38 if (k>n) 39 break; 40 vis[k]=1; 41 if (i%prime[j]==0) 42 break; 43 mu[k]=-mu[i]; 44 } 45 } 46 for (j=1;j<=g;j++) 47 for (i=prime[j],k=1;i<=n;i+=prime[j],k++) 48 v[i]+=mu[k]; 49 for (i=1;i<=n;i++) 50 sum[i]=sum[i-1]+v[i]; 51 } 52 53 int main() 54 { 55 int t,l,r,n,m; 56 ll ans; 57 mobius(); 58 scanf("%d",&t); 59 while (t--) 60 { 61 ans=0; 62 scanf("%d%d",&n,&m); 63 if (n>m) 64 swap(n,m); 65 for (l=1;l<=n;l=r+1) 66 { 67 r=min(n/(n/l),m/(m/l)); 68 ans+=1ll*(n/l)*(m/l)*(sum[r]-sum[l-1]); 69 } 70 printf("%lld\n",ans); 71 } 72 return 0; 73 }
luogu3455
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 11 const double eps=1e-8; 12 const ll inf=1e9; 13 const ll mod=1e9+7; 14 const int maxn=5e4+10; 15 16 int maxv=5e4; 17 int mu[maxn],zhi[maxn],sum_mu[maxn],cnt_zhi; 18 bool vis[maxn]; 19 20 int main() 21 { 22 int i,j,k,a,b,d,aa,bb,c,q,l,r; 23 ll sum; 24 for (i=2;i<=maxv;i++) 25 { 26 if (!vis[i]) 27 { 28 zhi[++cnt_zhi]=i; 29 mu[i]=-1; 30 } 31 for (j=1;j<=cnt_zhi;j++) 32 { 33 k=i*zhi[j]; 34 if (k>maxv) 35 break; 36 vis[k]=1; 37 if (i%zhi[j]==0) 38 break; 39 mu[k]=-mu[i]; 40 } 41 } 42 mu[1]=1; /// 43 for (i=1;i<=maxv;i++) 44 sum_mu[i]=sum_mu[i-1]+mu[i]; 45 46 scanf("%d",&q); 47 while (q--) 48 { 49 scanf("%d%d%d",&a,&b,&d); 50 aa=a/d; 51 bb=b/d; 52 c=min(aa,bb); 53 sum=0; 54 for (l=1,r=0;l<=c;l=r+1) 55 { 56 r=min(aa/(aa/l),bb/(bb/l)); 57 sum+=1ll*(sum_mu[r]-sum_mu[l-1])*(aa/l)*(bb/l); 58 } 59 printf("%lld\n",sum); 60 } 61 return 0; 62 }
luogu3327
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 11 const double eps=1e-8; 12 const ll inf=1e9; 13 const ll mod=1e9+7; 14 const int maxn=5e4+10; 15 16 int maxv=5e4; 17 int mu[maxn],zhi[maxn],sum_mu[maxn],cnt_zhi; 18 ll value[maxn]; 19 bool vis[maxn]; 20 21 int main() 22 { 23 int i,j,k,l,r,n,m,nm,q; 24 ll sum; 25 for (i=2;i<=maxv;i++) 26 { 27 if (!vis[i]) 28 { 29 zhi[++cnt_zhi]=i; 30 mu[i]=-1; 31 } 32 for (j=1;j<=cnt_zhi;j++) 33 { 34 k=i*zhi[j]; 35 if (k>maxv) 36 break; 37 vis[k]=1; 38 if (i%zhi[j]==0) 39 break; 40 mu[k]=-mu[i]; 41 } 42 } 43 mu[1]=1; /// 44 45 for (i=1;i<=maxv;i++) 46 sum_mu[i]=sum_mu[i-1]+mu[i]; 47 48 for (k=1;k<=maxv;k++) 49 { 50 for (l=1;l<=k;l=r+1) 51 { 52 r=k/(k/l); 53 value[k]+=1ll*(r-l+1)*(k/l); 54 } 55 } 56 57 scanf("%d",&q); 58 while (q--) 59 { 60 sum=0; 61 scanf("%d%d",&n,&m); 62 nm=min(n,m); 63 for (l=1;l<=nm;l=r+1) 64 { 65 r=min(n/(n/l),m/(m/l)); 66 sum+=1ll*(sum_mu[r]-sum_mu[l-1])*value[n/l]*value[m/l]; 67 } 68 printf("%lld\n",sum); 69 } 70 return 0; 71 }
luogu2522
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 11 const double eps=1e-8; 12 const ll inf=1e9; 13 const ll mod=1e9+7; 14 const int maxn=5e4+10; 15 16 int maxv=5e4; 17 int mu[maxn],zhi[maxn],sum_mu[maxn],cnt_zhi; 18 bool vis[maxn]; 19 20 ll cal(ll a,ll b,ll k) 21 { 22 ll d,aa,bb,c,l,r,sum=0; 23 aa=a/k; 24 bb=b/k; 25 c=min(aa,bb); 26 sum=0; 27 for (l=1,r=0;l<=c;l=r+1) 28 { 29 r=min(aa/(aa/l),bb/(bb/l)); 30 sum+=1ll*(sum_mu[r]-sum_mu[l-1])*(aa/l)*(bb/l); 31 } 32 return sum; 33 } 34 35 int main() 36 { 37 int i,j,k,q,a,b,c,d; 38 ll sum; 39 for (i=2;i<=maxv;i++) 40 { 41 if (!vis[i]) 42 { 43 zhi[++cnt_zhi]=i; 44 mu[i]=-1; 45 } 46 for (j=1;j<=cnt_zhi;j++) 47 { 48 k=i*zhi[j]; 49 if (k>maxv) 50 break; 51 vis[k]=1; 52 if (i%zhi[j]==0) 53 break; 54 mu[k]=-mu[i]; 55 } 56 } 57 mu[1]=1; /// 58 for (i=1;i<=maxv;i++) 59 sum_mu[i]=sum_mu[i-1]+mu[i]; 60 61 scanf("%d",&q); 62 while (q--) 63 { 64 scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); 65 printf("%lld\n",cal(b,d,k)-cal(a-1,d,k)-cal(b,c-1,k)+cal(a-1,c-1,k)); 66 } 67 return 0; 68 }
题目:
mobius+杜教筛
https://www.zybuluo.com/yang12138/note/1277248
mobius+容斥
https://blog.csdn.net/ZLH_HHHH/article/details/78191689
https://acm.ecnu.edu.cn/wiki/index.php?title=ACM-ICPC_2018_Shenyang_Online_Contest (ecnu wiki search competition)
https://blog.csdn.net/qkoqhh/article/details/82532516#commentsedit