快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0
102. Coprimes
time limit per test: 0.25 sec. 
memory limit per test: 4096 KB
For given integer N (1<=N<=104) 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
Input file contains integer N.
Output
Write answer in output file.
Sample Input
9
Sample Output
6
注意欧拉函数只需要统计质因子
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=100;
int n;
int prim[maxn],len;
void divide(){
    len=0;
    int tn=n;
    int r=sqrt(n+0.5)+1;
    for(int i=2;i<r;i++){
        if(tn%i==0){
            prim[len++]=i;
            while(tn%i==0)tn/=i;
        }
    }
    if(tn!=1)prim[len++]=tn;
}
int phi(){
    int ans=n;
    for(int i=0;i<len;i++){
        ans/=prim[i];
        ans*=prim[i]-1;
    }
    return ans;
}
int main(){
    while(scanf("%d",&n)==1){
        divide();
        int amount=phi();
        printf("%d\n",amount);
    }
    return 0;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号