bzoj 2818

记得以前是用容斥原理过的?好吧现在只记得奇加偶减了。。。

转化题目成求满足x/p和y/p互质的数对,那和上题就差不多了

先欧拉筛求出phi的前缀和a[i],依次枚举每个素数p[i],排除(1,1)答案就是sigma(a[n/p[i]]*2-1)

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(int i=l;i<=r;i++)
 3 #define dec(i,l,r) for(int i=l;i>=r;i--)
 4 #define link(x) for(edge *j=h[x];j;j=j->next)
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define inf 1e9
 7 #define ll long long
 8 #define succ(x) (1<<x)
 9 #define NM 10000000+5
10 using namespace std;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
14     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
15     return x*f;
16 }
17 int n,p[NM],phi[NM],tot;
18 ll a[NM],s;
19 bool check[NM];
20 int main(){
21     freopen("data.in","r",stdin);
22     n=read();
23     a[1]=phi[1]=1;
24     inc(i,2,n){
25         if(!check[i]){
26             p[++tot]=i;
27             phi[i]=i-1;
28         }
29         a[i]=a[i-1]+phi[i];
30         inc(j,1,tot){
31             if(p[j]*i>n)break;
32             check[p[j]*i]=true;
33             if(i%p[j])phi[i*p[j]]=phi[i]*phi[p[j]];
34             else{
35                 phi[i*p[j]]=phi[i]*p[j];
36                 break;
37             }
38         }
39     }
40 //    inc(i,1,n)printf("%d ",a[i]);printf("\n");
41     inc(i,1,tot)s+=a[(int)(0.999+n/p[i])]*2-1;
42     printf("%lld\n",s);
43     return 0;
44 }
View Code

 

你可能感兴趣的:(bzoj 2818)