我们用ϕ(n)表示欧拉函数
定义:ϕ(n)表示对于整数n,小于等于n中与n互质的数的个数
1.ϕ(n)为积性函数
证明:
此处证明需要用到下面计算方法1中的内容,建议先看后面再回过头来看这里
假设存在p,q,且p∗q=n
将n,p,q进行质因数分解
那么
因为
显然
这种方法也是常见的证明一个函数是积性函数的方法
2.
3.1到n中与n互质的数的和为
假设我们需要计算
分情况讨论
很明显,答案为1
根据素数的定义,答案为n-1
(仅有n与n不互质)
我们已经知道了n为素数的情况
不妨对n进行质因数分解
设
假设$k=1$
那么
证明:
考虑容斥,与一个数互素的数的个数就是这个数减去与它不互素的数的个数
因为p是素数,所以在中与其不互素的数为
得证
当时
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
int main()
{
LL N;
while(cin>>N&&N!=0)
{
int limit=sqrt(N),ans=N;
for(int i = 2; i <= limit ; ++i)
{
if(N%i==0) ans=ans/i*(i-1);
while(N%i==0) N=N/i;
}
if(N>1) ans=ans/N*(N-1);
printf("%d\n",ans);
}
return 0;
}
因为欧拉函数是积性函数
因此可以使用线性筛法
若p为素数,则
证明:
在1-p中,只有
若且p为素数
则
这一步同时利用了性质1和欧拉函数的积性
若,且p为素数,
则
证明:
没怎么看懂,丢一个链接
http://blog.csdn.net/Lytning/article/details/24432651
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN=1e6+10;
int prime[MAXN],tot=0,vis[MAXN],phi[MAXN],N=10000;
void GetPhi()
{
for(int i=2;i<=N;i++)
{
if(!vis[i])
{
prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&prime[j]*i<=N;j++)
{
vis[ i*prime[j] ] = 1;
if(i%prime[j]==0)
{
phi[ i*prime[j] ]=phi[i]*prime[j];
break;
}
else phi[ i*prime[j] ]=phi[i]*(prime[j]-1);
}
}
}
int main()
{
GetPhi();
cin>>N;
printf("%d\n",phi[N]);
return 0;
}
放几道水题
http://poj.org/problem?id=2407
http://poj.org/problem?id=2478
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有