思路:
裸的欧拉函数
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; int t,n,ans; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();} return x*f; } int get_phi(int x) { int sum=x; if(x%2==0) { while(x%2==0) x/=2; sum/=2; } for(int i=3;i*i<=x;i+=2) { if(x%i==0) { while(x%i==0) x/=i; sum=sum/i*(i-1); } } if(x>1) sum=sum/x*(x-1); return sum; } int main() { t=read(); while(t--) { n=read(); ans=get_phi(n); printf("%d\n",ans); } return 0; }
时间: 2024-10-18 04:11:08