您的位置:首页 > 大数据 > 人工智能

2012 Multi-University Training Contest 3:Arcane Numbers 1

2012-08-13 19:43 561 查看
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.

SampleInput

3
5 5
2 3
1000 2000


SampleOutput

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


Author

Vance and Shackler

//题意:问一个A进制下的有限小数嫩否在B进制下也是有限小数。

//一个小数x转换为A进制就是不断的除以A,可以记为x*A^(-p) 。 而这个有限小数要转化为 B进制下的小数,按进制转换规则就要不断乘以B,直到得到一个整数,最后将这个整数做B进制处理然后小数点向右移m位,m为乘以B的次数。 所以,A进制下的小数x*A(-p)一直乘以B后必须为整数,即x*(A^(-p))*(B^q)为整数,则要求B^q%A^p==0。其中p,q可以为任意值。 显然要求A的所有质因子都必须被B包含。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
#define maxn 1000000
#define LL __int64

bool x[maxn+10];
int prime[maxn+10],num=0;
void Init()
{
int i,tmp;
memset(x,false,sizeof(x));
x[0]=x[1]=true;
for(i=2;i<=maxn;i++)
if(!x[i])
{
prime[++num]=i;
tmp=i*2;
while(tmp<=maxn)
{
x[tmp]=true;
tmp+=i;
}
}
}

int main()
{
Init();
int T,t=0,i;
LL n,m;
LL A[100];
scanf("%d",&T);
while(T--)
{
int cnta=0;
scanf("%I64d%I64d",&n,&m);
printf("Case #%d: ",++t);
if(n==m)
{
puts("YES");
continue;
}
for(i=1;i<=num&&(LL)prime[i]*prime[i]<=n;i++)
if(n&&n%prime[i]==0)
{
A[++cnta]=prime[i];
while(n&&n%prime[i]==0)
n/=prime[i];
}
if(n>1)
A[++cnta]=n;
bool flag=true;
for(i=1;i<=cnta;i++)
if(m%A[i]!=0)
{
flag=false;
break;
}
printf("%s\n",flag?"YES":"NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: