Arcane Numbers 1

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 Vance’s 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.

InputThe first line contains a single integer T, the number of test cases. 
For each case, there’s a single line contains A and B. 
OutputFor 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

关键:只要B中包含A中的所有的素因子,就一定能找到h,是两种进制之间可以实现相互转换。

      利用 二进制小数转化成十进制小数的方法。

      假设一个A进制小数为 a,那么转化的时候,有个过程需要不断地重复 a =(a*B)% 1。(这里规定 %1 表示去除整数部分)(先将B转化成A进制,再放入此公式)

      (除去的整数部分,转化成B进制,即依次为B的小数部分)

      直到a 等于 0。即转化成功。

      若永远无法使 a 等于 0,即无法转化。

      那么在什么情况下,能够转化成功呢?

      假若 a的小数部分,最右端为一个非0数字(介于 1 ~ A-1之间)。

      我们可以从中随便找一个数字s ,最坏的情况是 s 与 A 互质。

      而上面式子可以写成 :$(a{\rm{ *}}B*{\rm{ }}B{\rm{ }}*{\rm{ }} \ldots {\rm{ }}*{\rm{ }}B\;)\% {\rm{ }}1 = 0$

      首先就需要满足(注意这里s、B均是A进制整数,并且是正常%A):$(s*B*B* \ldots *B)\% A = 0$

      很容易得出,上式其实是需要满足 :$(B*B* \ldots *B)\% A = 0$

      所以求的就是 A 的所有质因子,B是否含有?   若含有,则能转化。

      那么用一个技巧 求 gcd ,很简单就能解决此问题。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef long long ll;
 7 ll gcd(ll a,ll b){
 8     while(b){
 9         ll temp=b;
10         b=a%b;
11         a=temp;
12     }
13     return a;
14 }
15 int main(){
16     ll t;
17     scanf("%lld",&t);
18     for(int i=1;i<=t;i++){
19         ll a,b;
20         scanf("%lld%lld",&a,&b);
21         ll c=gcd(a,b);
22         while(c>1){      //这种处理方式很漂亮 
23             a/=c;
24             c=gcd(a,b);
25         }
26         if(a==1){
27             printf("Case #%d: YES\n",i);
28         }else{
29             printf("Case #%d: NO\n",i);
30         }
31     }
32 }

 

posted @ 2017-04-27 22:39  Elpsywk  阅读(151)  评论(0编辑  收藏  举报