【bzoj2818】 Gcd
http://www.lydsy.com/JudgeOnline/problem.php?id=2818 (题目链接)
题意
求给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对。
Solution
对于gcd(x,y)=p的数对个数,就相当于x/p和y/p互质。
细节
前缀和开LL
代码
// poj2478 #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #define LL long long #define inf 2147483640 #define MOD 10000 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=10000010; int phi[maxn],p[maxn],vis[maxn],n; LL s[maxn]; void calphi() { phi[1]=1; for (int i=2;i<=n;i++) { if (!vis[i]) {p[++p[0]]=i;phi[i]=i-1;} for (int j=1;j<=p[0];j++) { if (i*p[j]>n) break; vis[p[j]*i]=1; if (i%p[j]==0) {phi[p[j]*i]=phi[i]*p[j];break;} else phi[p[j]*i]=phi[p[j]]*phi[i]; } } for (int i=2;i<=n;i++) s[i]=s[i-1]+phi[i]; } int main() { scanf("%d",&n); calphi(); LL ans=p[0]; for (int i=1;i<=p[0];i++) { if (n/p[i]==1) break; ans+=2*s[n/p[i]]; } printf("%lld",ans); return 0; }
This passage is made by MashiroSky.