给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
标签:
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
一个整数N
如题
hint
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define N 10000000 5 using namespace std; 6 int flag[N+1000],prime[N+1000]; 7 long long phi[N+1000],ans; 8 int n,k; 9 void calcphi() 10 { 11 phi[1]=1; 12 for (int i=2;i<=n;i++) 13 { 14 if (!flag[i]) 15 { 16 prime[++k]=i; 17 phi[i]=i-1; 18 } 19 for (int j=1;j<=k&&i*prime[j]<=n;j++) 20 { 21 flag[i*prime[j]]=1; 22 if (i%prime[j]==0) 23 { 24 phi[i*prime[j]]=phi[i]*prime[j]; 25 break; 26 } 27 else phi[i*prime[j]]=phi[i]*(prime[j]-1); 28 } 29 } 30 } 31 32 int main() 33 { 34 scanf("%d",&n); 35 calcphi(); 36 for (int i=1;i<=n;i++) 37 phi[i]+=phi[i-1]; 38 for (int i=1;i<=k;i++) 39 ans+=phi[n/prime[i]]; 40 printf("%lld",ans*2-k); 41 return 0; 42 }
标签:
原文地址:http://www.cnblogs.com/DMoon/p/5225374.html