POJ 2407 Relatives 欧拉函数

题意:求phi(n)

#include<iostream>
#include<cstdio>
#include<cstring>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=105;
const int inf=0x3f3f3f3f;
int getphi(int x) {
    int ans=x;
    for(int i=2;i*i<=x;i++) {
       if(x%i==0) {
            ans-=ans/i;
            while(x%i==0) {
                x/=i;
            }
       }
    }
    if(x>1) {
        ans-=ans/x;
    }
    return ans;
}
int main() {
    int n;
    while(scanf("%d",&n) != EOF && n) {
        printf("%d\n",getphi(n));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41231363/article/details/80316854