-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
45 lines (42 loc) · 952 Bytes
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Author: RainAir
* Time: 2020-03-05 10:40:41
*/
#include<bits/stdc++.h>
#define fi first
#define se second
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
inline int phi(int n){
int q = std::sqrt(1.0*n);
int ans = n;
FOR(i,2,q){
if(!(n%i)){
while(!(n%i)) n /= i;
ans = ans/i*(i-1);
}
}
if(n > 1) ans = ans/n*(n-1);
return ans;
}
inline int gcd(int x,int y){
return !y ? x : gcd(y,x%y);
}
signed main(){
int T;scanf("%lld",&T);
while(T--){
int a,m;scanf("%lld%lld",&a,&m);
int g = gcd(a,m);
printf("%lld\n",phi(m/g));
}
return 0;
}