【POJ2478】Farey Sequence

【题目大意】

求在[1,n]范围内的(a,b)且a≠b,满足gcd(a,b)=1的个数

 

【题解】

欧拉函数裸题,无需多言。

 1 /*************
 2   POJ 2478
 3   by chty
 4   2016.11.3
 5 *************/
 6 #include<iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<cstdlib>
10 #include<ctime>
11 #include<cmath>
12 #include<algorithm>
13 using namespace std;
14 #define MAXN 1000010
15 int n,phi[MAXN],check[MAXN],prime[MAXN];
16 inline int read()
17 {
18     int x=0,f=1;  char ch=getchar();
19     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
20     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
21     return x*f;
22 }
23 void get()
24 {
25     phi[1]=1;  int cnt=0;
26     for(int i=2;i<=MAXN;i++)
27     {
28         if(!check[i])  {prime[++cnt]=i;  phi[i]=i-1;}
29         for(int j=1;j<=cnt&&prime[j]*i<=MAXN;j++)
30         {
31             check[i*prime[j]]=1;
32             if(i%prime[j])  phi[i*prime[j]]=phi[i]*(prime[j]-1);
33             else {phi[i*prime[j]]=phi[i]*prime[j];  break;}
34         }
35     }
36 }
37 int main()
38 {
39     freopen("cin.in","r",stdin);
40     freopen("cout.out","w",stdout);
41     get();
42     while(scanf("%d",&n)&&n)
43     {
44         long long ans=0;
45         for(int i=2;i<=n;i++)  ans+=phi[i];
46         printf("%lld\n",ans);
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/chty/p/6026549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值