[CodeForces - 1295D] Same GCDs 欧拉函数,GCD性质

【题目链接】:https://codeforces.com/problemset/problem/1295/D

【题意】

给定a,m (a,m<=10^10) 求gcd(a+x,m)=gcd(a,m) (0<=x<m) 的x的个数

【题解】

 

 

#include <bits/stdc++.h>
using namespace std;
long long n,m,gc,tot;
typedef long long ll;
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
ll euler(ll nn){    
     ll res=nn,a=nn;  
     for(ll i=2;i*i<=a;i++){  
         if(a%i==0){  
             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出   
             while(a%i==0) a/=i;  
         }  
     }  
     if(a>1) res=res/a*(a-1);  
     return res;  
}  
int main(){
    int T;
    cin>>T;
    while (T--){
        tot=0;
        scanf("%lld%lld",&n,&m);
        gc=gcd(m,n);
        ll q=euler(m/gc);
        printf("%lld\n",q);
    }
}

 

posted @ 2020-08-12 15:50  conver^_^  阅读(131)  评论(0编辑  收藏  举报