设为首页 收藏本站
查看: 1151|回复: 0

[经验分享] codechef(MAY15)第三题Chef and Prime Divisors

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-26 07:36:45 | 显示全部楼层 |阅读模式

http://www.codechef.com/MAY15/problems/CHAPD





You are given two positive integers – A and B. You have to check whether A is divisible by all the prime divisors of B.



Input



The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.


For each test case, you are given two space separated integers – A and B.



Output



For each test case, output "Yes" (without quotes) if A contains all prime divisors of B, otherwise print "No".



Constraints



  • 1 ≤ T ≤ 104
  • 1 ≤ A, B ≤ 1018


Subtasks



  • Subtask 1 (20 points):1 ≤ B ≤ 107
  • Subtask 2 (30 points):1 ≤ A ≤ 107
  • Subtask 3 (50 points): Original constraints


Example


Input:
3
120 75
128 16
7 8
Output:
Yes
Yes
No


Explanation



Example case 1. In the first case 120 = 23*3*5 and 75 = 3*52. 120 is divisible by both 3 and 5. Hence, we will print "Yes"


Example case 2. In the second case both 128 and 16 are powers of two. Hence, the answer is "Yes"


Example case 3. In the third case 8 is power of two and 7 is not divisible by 2. So, the answer is "No"






题意:给定数字A和B,判断A是否能被B的所有质因子整除。


思路:


所有数都可以看成是被很多的质数相乘,所以把B也看成这样的数,由于B和A的最大公约数一定能被A除,所以想到一直GCD处理B,如果最后B是1就说明整个B的因素都能被A整除


#include<iostream>
using namespace std;
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
long long T;
cin >> T;
while(T--)
{
long long a,b;
cin >> a >> b;
long long num = gcd(a,b);
while(num>1)
{
b = b/num;
num = gcd(a,b);
}
if(b == 1)
cout<<&quot;Yes&quot;<<endl;
else
cout<<&quot;No&quot;<<endl;
}
return 0;
}




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143589-1-1.html 上篇帖子: Chef and Prime Divisors(GCD) 下篇帖子: 【codechef】Chef and Polygons(灵活题,坑题)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表