The Euler function

79 篇文章 0 订阅
0 篇文章 0 订阅

Link:http://acm.hdu.edu.cn/showproblem.php?pid=2824

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3889    Accepted Submission(s): 1613


Problem Description
The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
 

Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
 

Output
Output the result of (a)+ (a+1)+....+ (b)
 

Sample Input
  
  
3 100
 

Sample Output
  
  
3042
 

Source
 



大概:求a~b段欧拉值和,要用到筛法欧拉函数。

筛选:1)如果n为素数,φ(n)=n-1;

           2)如果m,n互质,φ(n*m)=φ(n)*φ(m);


核心就是当m%p==0:

f(m*p) = f(m) * p;//因为(p-1)是在f(m)中所以是乘p;

否则:

f(m*p) = f(m) * (p-1);

因为Euler函数是积性函数所以


综合上面两个式子就可以知道核心的推导公式如何得来


   

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<ctime>  
  4. using namespace std;  
  5. typedef  __int64 INT;  
  6. #define N 3000001  
  7. int prime[216818],phi[N];  
  8. bool a[N];  
  9. void is_prime()  
  10. {  
  11.     int i,j,k;  
  12.     k=0;  
  13.     for(i=2;i<N;i++)  
  14.     {  
  15.        if(!a[i])  
  16.        {  
  17.           prime[k++]=i;  
  18.           phi[i]=i-1;  
  19.        }  
  20.        for(j=0;j<k&&i*prime[j]<N;j++)  
  21.        {  
  22.          a[prime[j]*i]=1;  
  23.          if(i%prime[j])  
  24.              phi[i*prime[j]]=phi[i]*(prime[j]-1);  
  25.          else  
  26.          {  
  27.              phi[i*prime[j]]=phi[i]*prime[j];  
  28.              break;  
  29.          }  
  30.          
  31.        }  
  32.       
  33.     }  
  34. }  
  35. int main()  
  36. {  
  37.     //freopen("c_in.txt","r",stdin);  
  38.     //freopen("myout5.txt","w",stdout);  
  39.     int a,b,i;  
  40.     INT sum;  
  41.     is_prime();  
  42.     while(cin>>a>>b)  
  43.     {  
  44.         sum=0;  
  45.         for(i=a;i<=b;i++)  
  46.             sum+=phi[i];  
  47.         printf("%I64d\n",sum);  
  48.       
  49.     }  
  50.     //printf("%lfMS\n",(double)clock()/CLOCKS_PER_SEC);  
  51.     return 0;  
  52. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值