codechef-Chef and Prime Divisors

codechef-Chef and Prime Divisors

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 T test 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

Input:
3
120 75
128 16
7 8

Output:
Yes
Yes
No

Explanation

Example case 1. In the first case 120 = 2^3*3 * 5 and 75 = 3 * 5^2. 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”

题目大意:给定两个整数A和B,请判断A是否能整除B所有的质因数。

题目链接:Chef and Prime Divisors

题目思路:我们知道,所有的数都可以由若干个质数组成。如果A能整除B所有的质因数也就是说,A能整除所有A和B的最大公约数。最后,如果B为1,说明A能整除B所有的质因数,否则不能。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
long long  gcd (long long a,long long b)
{
    return (b == 0) ? a : gcd(b, a % b);
} 
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        long long a,b;
        cin >> a >> b;
        long long num = gcd(a,b);
        while(num > 1)
        {
            b = b / num;
            num = gcd(a,b);
        }
        if (b == 1) cout << "Yes\n";
        else    cout << "No\n";
    }
    return 0;
}

你可能感兴趣的:(数学,CodeChef)