BZOJ2818:Gcd(莫比乌斯反演)

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7

Solution 

同BZOJ2820……

Code

 1 #include<iostream>
 2 #include<cstdio>
 3 #define N (10000000)
 4 using namespace std;
 5 
 6 int n,m,vis[N+5],prime[N+5],mu[N+5],cnt;
 7 long long sum[N+5];
 8 
 9 void Get_mu()
10 {
11     mu[1]=1;
12     for (int i=2; i<=n; ++i)
13     {
14         if (!vis[i]){prime[++cnt]=i; mu[i]=-1;}
15         for (int j=1; j<=cnt && prime[j]*i<=n; ++j)
16         {
17             vis[prime[j]*i]=true;
18             if (i%prime[j]==0) break;
19             mu[prime[j]*i]=-mu[i];
20         }
21     }
22     for (int i=1; i<=cnt; ++i)
23         for (int j=1; j*prime[i]<=n; ++j)
24             sum[j*prime[i]]+=mu[j];
25     for (int i=1; i<=n; ++i) sum[i]+=sum[i-1];
26 }
27 
28 long long Calc(int n,int m)
29 {
30     long long ans=0; if (n>m) swap(n,m);
31     for (int l=1,r; l<=n; l=r+1)
32     {
33         r=min(n/(n/l),m/(m/l));
34         ans+=(sum[r]-sum[l-1])*(n/l)*(m/l);
35     }
36     return ans;
37 }
38 
39 int main()
40 {
41     scanf("%d",&n);
42     Get_mu();
43     printf("%lld\n",Calc(n,n)); 
44 }
posted @ 2018-09-07 10:25  Refun  阅读(198)  评论(0编辑  收藏  举报