Description
You are given two positive integers – A and B. You have to check whether A is divisible by all the prime divisors
of B.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases
follows.
For each test case, you are given two space separated integers – A and B.
Output
For each test case, output "Yes" (without quotes) if A contains all prime divisors of B, otherwise print "No".
Constraints
- 1 ≤ T ≤ 104
- 1 ≤ A, B ≤ 1018
Subtasks
- Subtask 1 (20 points):1 ≤ B ≤ 107
- Subtask 2 (30 points):1 ≤ A ≤ 107
- Subtask 3 (50 points): Original constraints
题意:输入两个数A,B,问A是否能被B所有的素数因子整除即A的因子中是否包含B所有的素数因子。
思路:当时比赛时首先想到的是先打素数表然后找出B的所有素数因子且同时判断其素数因子是否能被A整除,可是A,B的范围太大了,10^18!!!素数表根本打不出来呀23333!!!要是先找出B的因子再判断其是不是素数也肯定会超时呀,当时就傻眼了2333!!!后来比完赛请教做出来的大神,大神说了,只要判断B的因子是不是A的因子就可以了没必要看其因子是不是素数。对呀,只要B的所有因子是A的因子就够了呀管他是不是素数呢(心想:当时自己的脑子是被驴给踢了么,想那么复杂干嘛,直接判断是不是其因子就好了呀2333!!!)。要想判断B的所有因子是不是A的因子只需先求出它俩的公约数,然后让B除以公约数,再求A与此时的B的公约数.....不断反复下去,直到A与B互质,此时如果B=1就说明此组A,B符合题意,否则,不符合。
Input:
3 120 75 128 16 7 8 Output:
Yes Yes No
Explanation
Example case 1. In the first case 120 = 23*3*5 and 75 = 3*52. 120 is divisible by both 3 and 5. Hence, we will print
"Yes"
Example case 2. In the second case both 128 and 16 are powers of two. Hence, the answer is "Yes"
Example case 3. In the third case 8 is power of two and 7 is not divisible by 2. So, the answer is "No"
<span style="font-size:18px;">#include <cstdio>
#define ll long long
using namespace std;
ll gcd(ll a,ll b) //辗转相除法求最大公约数(a相当于较大数,b相当于较小数)
{
return b?gcd(b,a%b):a;
}
int main()
{
int t;
ll a,b,g;
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld",&a,&b);
g=gcd(a,b);
while(g!=1) //只要两个数不互质
{
b/=g;
g=gcd(a,b);
}
if(b==1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}</span>