链接:https://codeforces.com/contest/1295/problem/D
You are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).
Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.
Input
The first line contains the single integer TT (1≤T≤501≤T≤50) — the number of test cases.
Next TT lines contain test cases — one per line. Each line contains two integers aa and mm (1≤a<m≤10101≤a<m≤1010).
Output
Print TT integers — one per test case. For each test case print the number of appropriate xx-s.
Example
input
Copy
3 4 9 5 10 42 9999999967
output
Copy
6 1 9999999966
Note
In the first test case appropriate xx-s are [0,1,3,4,6,7][0,1,3,4,6,7].
In the second test case the only appropriate xx is 00.
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m,t,k=0,a,l,p;
int main()
{
cin>>t;
while(t--)
{
cin>>a>>m;
n=m/__gcd(a,m);
k=n;
for(long long i=2;i*i<=n;i++)
{
if(n%i==0)
{
k/=i;
k*=(i-1);
while(n%i==0)
{
n/=i;
}
}
}
if(n!=1)
k-=k/n;
cout<<k<<endl;
}
return 0;
}