[codeforces 1295D] Same GCDs 欧拉函数
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1295/problem/D
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
D - Same GCDs | GNU C++11 | Accepted | 61 ms | 0 KB |
思路摘自https://blog.csdn.net/weixin_44091178/article/details/104114949
欧拉函数证明,详见此文https://zhuanlan.zhihu.com/p/56548135
#include <stdio.h>
#define LL long long
LL a,m,prime[15000];
int cnt;
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
void divide(LL x){
int i;
for(i=2;(LL)i*i<=x;i++)
if(x%i==0){
prime[++cnt]=i;
while(x%i==0)x/=i;
}
if(x>1)prime[++cnt]=x;
}
LL phi(LL x){
int i;
LL ans=x;
for(i=1;i<=cnt;i++)
ans/=prime[i],ans*=(prime[i]-1);
return ans;
}
int main(){
int t;
LL d;
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&a,&m);
d=gcd(a,m),cnt=0;
divide(m/d);
printf("%lld\n",phi(m/d));
}
return 0;
}