Arcane Numbers 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1687 Accepted Submission(s): 528

Problem Description
Vance and Shackler like playing games. One day, they are playing a game called “arcane numbers”. The game is pretty simple, Vance writes down a finite decimal under base A, and then Shackler translates it under base B. If Shackler can translate it into a finite decimal, he wins, else it will be Vances win. Now given A and B, please help Vance to determine whether he will win or not. Note that they are playing this game using a mystery language so that A and B may be up to 10^12.

Input
The first line contains a single integer T, the number of test cases.
For each case, theres a single line contains A and B.

Output
For each case, output NO if Vance will win the game. Otherwise, print YES. See Sample Output for more details.

Sample Input
3
5 5
2 3
1000 2000

Sample Output
Case #1: YES
Case #2: NO
Case #3: YES

Author
Vance and Shackler

Source
2012 Multi-University Training Contest 3

Recommend
zhoujiaqi2010

nnkk1
abab

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
 
using namespace std;
 
int const maxm=1009999;
int prime[maxm];
long long pri[maxm];
int sum;
void sievePrime(){   //
    int j;
    int i;
    for(i=0;i<maxm;i++)
        prime[i]=1;
    prime[0]=0;
    prime[1]=0;
    int max=sqrt(maxm*1.0);
    for(i=2;i<=max;i++)
    {
        if(prime[i])
        for(j=i+i;j<maxm;j=j+i)
        {
            prime[j]=0;
        }
    }
    for(i = 0, j = 0; i < maxm; i ++){
        if(prime[i]){
            pri[j++] = i;
        }
    }
    sum = j;
}
 
int main(){
    int t;
    int i, j;
    long long a, b;
    int cnt = 0;
    sievePrime();
    scanf("%d", &t);
    while(t --){
        cnt ++;
        scanf("%I64d %I64d", &a, &b);
        bool flag = true;
        for(i = 0; i < sum && pri[i] <= a; i ++){
            if(a % pri[i] == 0){
                if(b % pri[i]){
                    flag = false;
                    break;
                }
                while(a % pri[i] == 0){
                    a /= pri[i];
                }
            }
        }
        if(a != 1 && b % a != 0){  //ab
            flag = false;
        }
        printf("Case #%d: ", cnt);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}