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 ,很简单就能解决此问题。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
while(b){
ll temp=b;
b=a%b;
a=temp;
}
return a;
}
int main(){
ll t;
scanf("%lld",&t);
for(int i=;i<=t;i++){
ll a,b;
scanf("%lld%lld",&a,&b);
ll c=gcd(a,b);
while(c>){ //这种处理方式很漂亮
a/=c;
c=gcd(a,b);
}
if(a==){
printf("Case #%d: YES\n",i);
}else{
printf("Case #%d: NO\n",i);
}
}
}

Arcane Numbers 1的更多相关文章

  1. 2012 #3 Arcane Numbers

    Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  2. HDU 4320 Arcane Numbers 1 (数论)

    A - Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. HDU 4320 Arcane Numbers 1 (质因子分解)

    题目:传送门. 题意:将一个A进制下的有限小数转化为B进制看是否仍为有限小数. 题解:一个A进制的小数可以下次 左移动n位变成A进制整数然后再将其转化为B进制即可 即B^m/A^n要整除,因此A的质因 ...

  4. HDU 4320 Arcane Numbers 1(质因子包含)

    http://acm.hdu.edu.cn/showproblem.php?pid=4320 题意: 给出A,B,判断在A进制下的有限小数能否转换成B进制下的有限小数. 思路: 这位博主讲得挺不错的h ...

  5. 数论(GCD) HDOJ 4320 Arcane Numbers 1

    题目传送门 题意:有一个A进制的有限小数,问能否转换成B进制的有限小数 分析:0.123在A进制下表示成:1/A + 2/(A^2) + 3 / (A^3),转换成B进制就是不断的乘B直到为0,即(1 ...

  6. HDU 4321 Arcane Numbers 2

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4321 ----------------------------------------------- ...

  7. 2012多校3.A(用O(log(n))判断b^k % a == 0)

    Arcane Numbers 1 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  8. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

随机推荐

  1. python cookbook 字符串和文本

    使用多个界定符分隔字符串 import re line = 'asdf fjdk; afed, fjek,asdf, foo' print(re.split(r'[;,\s]\s*', line)) ...

  2. spring AOP简单实现代码存放

    @Before:使用Before增强处理只能在目标方法执行之前织入增强,如果Before增强处理没有特殊处理,目标方法总会自动执行,如果Before处需要阻止目标方法的执行,可通过抛出一个异常来实现. ...

  3. [原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  4. 【leetcode刷题笔记】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. mysql 数据库备份方案及策略

    由于mysql存在多种数据库备份方式,而且各有利弊,对于我们初学者来说,选择合适的备份方式确实有些困难.个人觉得,首先要基于公司的需求,考虑能够容忍丢失多少数据.花多少人力时间成本等,这是我们制定备份 ...

  6. Android系统篇之—-编写系统服务并且将其编译到系统源码中【转】

    本文转载自:http://www.wjdiankong.cn/android%E7%B3%BB%E7%BB%9F%E7%AF%87%E4%B9%8B-%E7%BC%96%E5%86%99%E7%B3% ...

  7. mysql 索引技巧

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...

  8. 在Delphi中使用ShellExecute(handle, 'open', PChar(fname),nil, nil, SW_HIDE)函数应注意的问题

    在Delphi中使用ShellExecute(handle, 'open', PChar(fname),nil, nil, SW_HIDE)函数应注意的问题: 一.对一般vcl程序及isapi dll ...

  9. adb命令(二)

    1.获取手机型号指令 adb shell cat /system/build.prop | findstr "ro.product.model" 2.获取手机处理器信息 adb s ...

  10. 《java编程思想》:散列的原理

    以实现一个简单的HashMap为例,详细讲解在code之中. 简单解释散列原理: 1.map中内建固定大小数组,但是数组并不保存key值本身,而是保存标识key的信息 2.通过key生成数组角标,对应 ...