uva 11889 Benefit
2016-07-23 15:55
330 查看
原题:
Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of his money. He believes that finding such a number is hard enough that dissuades students from paying that. You should write a program that help poor students giving the appropriate amount of money to Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.
Input
The first line begin with an integer T (T ≤ 100000), the number of tests. Each test that comes in a
separate line contains two integers A and C (1 ≤ A,C ≤ 10 7 ).
Output
Print the lowest integer B such that LCM(A,B) = C in a single line. If no such integer exists, print
‘NO SOLUTION’ instead. (Quotes for clarity)
Sample Input
3
2 6
32 1760
7 16
Sample Output
3
55
NO SOLUTION
中文:
就是给你两个数a和c,现在问你其中c是a和b的最小公倍数。现在问你b最小是多少?如果没有b,那么书输出NO SOLUTION。
解答:
这题还有点技巧,一时没绕过来。首先可以判断,a和b的最小公倍数是c,那么c能整除a,而且c能整除b,否则就是没结果。
首先设b’=c/a,如果gcd(a,b’)==1,那么说明a和b’互质,两个互质的数的最小公倍数就是他俩的乘积,如果gcd(a,b’)!=1,那么咱设这个结果是g,由最小公倍数的公式可以得到
a×b’/g=a×(c/a)/g,结果不等于c,因为多除了一个g。现在我们要想个办法把公因子g去掉,可以让
Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of his money. He believes that finding such a number is hard enough that dissuades students from paying that. You should write a program that help poor students giving the appropriate amount of money to Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.
Input
The first line begin with an integer T (T ≤ 100000), the number of tests. Each test that comes in a
separate line contains two integers A and C (1 ≤ A,C ≤ 10 7 ).
Output
Print the lowest integer B such that LCM(A,B) = C in a single line. If no such integer exists, print
‘NO SOLUTION’ instead. (Quotes for clarity)
Sample Input
3
2 6
32 1760
7 16
Sample Output
3
55
NO SOLUTION
中文:
就是给你两个数a和c,现在问你其中c是a和b的最小公倍数。现在问你b最小是多少?如果没有b,那么书输出NO SOLUTION。
#include <bits/stdc++.h> using namespace std; int gcd(int a,int b) { if(a%b==0) return b; return gcd(b,a%b); } int main() { ios::sync_with_stdio(false); int a,b,c; int t; cin>>t; while(t--) { cin>>a>>c; if(a==1) { cout<<c<<endl; continue; } if(c%a!=0) cout<<"NO SOLUTION"<<endl; else { b=c/a; int g=gcd(a,b); while(g!=1) { a=a/g; b*=g; g=gcd(a,b); } cout<<b<<endl; } } return 0; }
解答:
这题还有点技巧,一时没绕过来。首先可以判断,a和b的最小公倍数是c,那么c能整除a,而且c能整除b,否则就是没结果。
首先设b’=c/a,如果gcd(a,b’)==1,那么说明a和b’互质,两个互质的数的最小公倍数就是他俩的乘积,如果gcd(a,b’)!=1,那么咱设这个结果是g,由最小公倍数的公式可以得到
lcm(a,b)=a×b’/gcd(a,b’)
所以lcm(a,b’)=a×b’/g。
这说明a和b’有最大公因子g,a和b’若是按照最小公倍数的公式去计算等价于a×b’/g=a×(c/a)/g,结果不等于c,因为多除了一个g。现在我们要想个办法把公因子g去掉,可以让
a=a/g
让b’=b’×g
这样做乘积的结果还是ca/g×b’×g==a×b’
只不过公因数不会是g了,然后每次循环这个过程,直到g==1,就构造出了两个互质的数,输出b即可。相关文章推荐
- windows 下wamp环境2 配置之mysql的安装
- Docker镜像文件存储结构
- 朋友圈柏拉图性格标签生成器源码防封版
- 二、词法结构
- 剑指offer 37题 【时间空间效率的平衡】两个链表的第一个公共结点
- 杭电5562之Clarke and food
- 【poj1995】Raising Modulo Numbers
- HTML5和CSS3扁平化风格博客
- scala 学习笔记4 cache 和persist的区别
- EditText 对手机号码的处理
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 地址解析协议ARP,网络层协议IP、ICMP协议
- 第一章 银联8583报文解析
- 2016夏季练习——Prim
- Codeforces Round #364 (Div. 2) D 数学推导
- 使用SurfaceBiew录制视频
- android面试专题(6)
- 仿京东,淘宝,顶部自动轮播图
- 云计算-云山雾罩不计算
- android仿腾讯小火箭案例