又搞了一道容斥原理。
题目:求【1,n】区间对m互质的数有多少个?
#include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL __int64 const int maxn = 1e5+8; LL a[maxn],cn,numpri[maxn],vis[maxn],dis[maxn]; LL n,m; LL f[maxn]; void getprim(){ cn = 0; for(LL i=2;i<10000;i++){ if(!dis[i]){ numpri[cn++] = i; for(LL j=2*i;j<=10000;j+=i) dis[j] = 1; } } } LL getans(LL Max,LL d){ vector <int > v; LL sum = 0; for(LL j=0;j<cn&&numpri[j] * numpri[j] <=d;j++){ if(d % numpri[j] == 0){ v.push_back(numpri[j]); while(d % numpri[j] == 0)d /= numpri[j]; } } if(d!=1)v.push_back(d); int len = v.size(); //cout << len <<endl; for(int j=0;j<(1<<len);j++){ LL op =0,ans = 1; for(int k=0;k<len;k++){ if((1<<k)&j){ op++; ans*=v[k]; } } if(op&1)sum-= Max/ans; else sum += Max/ans; } return sum; } int su = 0; int main(){ LL T; LL a,b,c; scanf("%I64d",&T); getprim(); while(T--){ cin >> a>>b>>c; // cout << getans(b,c)<<endl; printf("Case #%d: %I64d\n",++su,getans(b,c) - getans(a-1,c)); } }
原文:http://blog.csdn.net/wyl_zheyang/article/details/40833297