D. Same GCDs

194 篇文章 1 订阅
44 篇文章 0 订阅

链接: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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值