题目链接:
http://acm.gdufe.edu.cn/Problem/read/id/1086
互质数
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
For given integer N (1<=N<=10^8) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1).
Input:
The input includes several cases. For each case,the input contains integer N.
Output:
For each case,print answer in one line.
Sample Input:
9
Sample Output:
6
此题用到欧拉函数:
在数论中,对正整数n,欧拉函数是小于等于n的数中与n互质的数的数目。
代码:
#include <stdio.h>
int Eu(int n){
int ans=n;
for(int i=2;i<=n;i++){
if(n%i==0){
ans-=ans/i;//x*(1-1/p)=x-x/p
while(n%i==0)
n/=i;
}
if(n==1)
break;
}
return ans;
}
int main(){
int n;
while(scanf("%d",&n)==1){
printf("%d\n",Eu(n));
}
return 0;
}