质因数:arcane number1

来源:互联网 发布:k30平板荷载试验算法 编辑:程序博客网 时间:2024/06/05 03:58

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 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.
 

Input
The first line contains a single integer T, the number of test cases.
For each case, there’s 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
35 52 31000 2000
 

Sample Output
Case #1: YESCase #2: NOCase #3: YES
 

Author
Vance and Shackler
 

Source
2012 Multi-University Training Contest 3
 

Recommend
zhoujiaqi2010


n进制的小数,是n的k次方分之一的小数的组合,k从1到无穷大

如果a有的质因数b没有,则a无法用b表示

#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){  //a中有可能还包含b中没有的质因数            flag = false;        }        printf("Case #%d: ", cnt);        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}


原创粉丝点击