ACM常用数论之欧拉函数
2017-04-29 21:36
330 查看
数论,对正整数n,欧拉函数是小于n的正整数中与n互质的数的数目(φ(1)=1)。此函数以其首名研究者欧拉命名(Euler’so totient function),它又称为Euler’s totient function、φ函数、欧拉商数等。 例如φ(8)=4,因为1,3,5,7均和8互质。 从欧拉函数引伸出来在环论方面的事实和拉格朗日定理构成了欧拉定理的证明。
欧拉函数公式:euler(x) = x*(1-1/p1)(1-1/p2)……(1-1/pn),p为x的质因数。用公式法求解代码
打表求法,用的比较多
用法一 :由欧拉函数求解也可以求出,一个数x的所有质因数的和euler(n)*n/2
这里涉及到一个问题;
欧拉数在n>2时,必定为偶数
首先我们知道因数分解定理,设
证明如下。看不懂,懂这个性质就行
n=Πpi^αi
Φ(n)=Π(pi^αi-pi^(αi-1))
如果n=2^α,α≥2
则Φ(n)=2^α-2^(α-1),=2^(α-1)
为偶数;
如果n>2,而且至少有一个奇素数p
则 p^α-p^(α-1) 为偶数(α≥1)
(因为 p^α与p^(α-1) 均为奇数)
故若N>2,则Φ(N)必定是偶数.
用法二:质数n的euler(n) = n-1;
例题
hdu2824
The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7245 Accepted Submission(s): 3005
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
Output
Output the result of (a)+ (a+1)+….+ (b)
Sample Input
3 100
Sample Output
3042
求两个数之间所有数的欧拉数的和,
即e(a)+……e(b);
欧拉函数公式:euler(x) = x*(1-1/p1)(1-1/p2)……(1-1/pn),p为x的质因数。用公式法求解代码
int euler(int n){ int res=n,a=n; for(int i=2;i*i<=a;i++){ if(a%i==0){ res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出 while(a%i==0) a/=i; } } if(a>1) res=res/a*(a-1); return res; }
打表求法,用的比较多
void Euler() { euler[1]=1; for(int i=2;i<MAX;i++) euler[i]=i; for(int i=2;i<MAX;i++) if(euler[i]==i) for(int j=i;j<MAX;j+=i) euler[j]=euler[j]/i*(i-1); }
用法一 :由欧拉函数求解也可以求出,一个数x的所有质因数的和euler(n)*n/2
这里涉及到一个问题;
欧拉数在n>2时,必定为偶数
首先我们知道因数分解定理,设
证明如下。看不懂,懂这个性质就行
n=Πpi^αi
Φ(n)=Π(pi^αi-pi^(αi-1))
如果n=2^α,α≥2
则Φ(n)=2^α-2^(α-1),=2^(α-1)
为偶数;
如果n>2,而且至少有一个奇素数p
则 p^α-p^(α-1) 为偶数(α≥1)
(因为 p^α与p^(α-1) 均为奇数)
故若N>2,则Φ(N)必定是偶数.
用法二:质数n的euler(n) = n-1;
例题
hdu2824
The Euler function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7245 Accepted Submission(s): 3005
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
Output
Output the result of (a)+ (a+1)+….+ (b)
Sample Input
3 100
Sample Output
3042
求两个数之间所有数的欧拉数的和,
即e(a)+……e(b);
#include<stdio.h>
#define MAX 3000005
__int64 euler[MAX];
void Euler() { euler[1]=1; for(int i=2;i<MAX;i++) euler[i]=i; for(int i=2;i<MAX;i++) if(euler[i]==i) for(int j=i;j<MAX;j+=i) euler[j]=euler[j]/i*(i-1); }
int main()
{
Euler();
int a,b;
while(~scanf("%d%d",&a,&b))
{
__int64 sum=0;
for(int i=a;i<=b;i++)
{
sum+=euler[i];
}
printf("%I64d\n",sum);
}
return 0;
}
相关文章推荐
- 【ACM_数论初步】欧拉函数
- ACM常用模板——数论
- ACM第一课---数论之欧拉函数
- ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭)
- 数论常用内容——欧拉函数
- ACM数论常用知识完全解读
- ACM: 扩展欧几里德+欧拉函数 数论…
- ACM:数论专题(5)——欧拉函数
- ACM第一课---数论之欧拉函数
- ACM常用数论知识 总结
- 程序设计竞赛(acm)常用数据结构
- 数论--欧拉函数的整理(悔惜晟)
- 【数论内容】线性筛素数,线性筛欧拉函数,求前N个数的约数个数
- ACMSTEP 2.1.7 Leftmost Digit // 数论 求 N^N最高次位
- ACM的常用算法(西交POJ)
- http://acm.hdu.edu.cn/showproblem.php?pid=1787 典型 欧拉函数(为何c++ 那样会RE)
- 数论之求欧拉函数
- POJ2478 Farey Sequence 欧拉函数与素数 数论题
- ACM常用算法及其相应的练习题
- 数论基础_欧拉函数