标签:names 欧拉函数 max target 代码 math.h 题目 targe ++
对正整数n,欧拉函数是小于等于n的正整数中与n互质的数的数目(φ(1)=1)。
性质:
代码:
int euler(int n)
{
int res=n,m=sqrt(n+0.5);
for(int i=2;i<=m&&n!=1;++i)
{
if(n%i==0)
{
res=res/i*(i-1);
while(n%i==0)
n/=i;
}
}
if(n!=1)
res=res/n*(n-1);
return res;
}
题目链接:
https://cn.vjudge.net/problem/HDU-1787
代码:
#include <cstdio>
#include <math.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=32768;
int euler(int n)
{
int res=n,m=sqrt(n+0.5);
for(int i=2;i<=m&&n!=1;++i)
{
if(n%i==0)
{
res=res/i*(i-1);
while(n%i==0)
n/=i;
}
}
if(n!=1)
res=res/n*(n-1);
return res;
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
printf("%d\n",n-1-euler(n));
return 0;
}
标签:names 欧拉函数 max target 代码 math.h 题目 targe ++
原文地址:https://www.cnblogs.com/judp/p/10773638.html