acm杭电acm5974 A Simple Math Problem 数论(最大公约数最小公约数,解方程x )
来源:互联网 发布:mac firefly安装 编辑:程序博客网 时间:2024/06/08 18:47
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5974
A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1646 Accepted Submission(s): 469
Problem Description
Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least Common Multiple (X, Y) =b
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.
Output
For each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of "No Solution"(without quotation).
Sample Input
6 8798 10780
Sample Output
No Solution308 490
Source
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
定理:
1、对于两个数 x, y ,最大公约数为 gcd ( x, y ) ,最小公倍数为 lcm ( x, y ),
并且x*y = gcd ( x, y )* lcm ( x, y )2、若 x 与 y 互质,则 x+y 与 x*y 也互质,互质的意思是两个数的最大公约数是1.
证明(反证法):
3、若 x ,y 的最大公约数为d,则 x/d 与 y/d 是互质的。设 a = x + y
b = x * y
假设a ,b 不互质,则一定存在公因数 d > 1 ;则a / d = x / d + y / d (两边同时初以d);
由此可见 x 与 y 也存在公因数 d 。
这与前提“x ,y 互质” 矛盾。
所以 x + y 与 x*y 互质
证明(反证法):
设 i = x/dj = y/d
假设 i 与 j 不互质, 则存在公约数 m,
既然 m是 i 与 j 的公约数,那么也应该是 x ,y 的公约数才对
那此时的x, y的最大公约数成了 d*m ,与前提相矛盾所以命题成立
已知:
x + y = ax * y = b * gcd(x,y) (定理1可得)设 t = gcd ( x, y )i = x / t ;j = y / t ;( i 与 j 是互质的,定理3可得)则:
i * t + j * t = a ;i * j * t = b ;即:
( i + j ) * t = a
i * j * t = b ;
(因为 i j 互质,所以 i+j 与 i*j 互质,定理2可得)
所以a和b的最大公约数等于 t
由此可见:
gcd ( a, b ) == t == gcd ( x, y )
问题就迎刃而解了。
回到已知:
x + y = ax * y = b * gcd(x,y) = b * gcd(a,b)两式联立可得一个方程解方程 x^2 - a*x + b*gcd(a,b) = 0
只要方程有整数解即可
代码:
#include<stdio.h>#include<math.h>int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}int main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF){int t=gcd(a,b);//解方程 x^2 - a*x + b*t = 0double v=a*a-4*b*t;if(v<0 || v>=0&&(int)sqrt(v)*sqrt(v)!=v) //无解 puts("No Solution");else {int x1=a-sqrt(v);int x2=a+sqrt(v);printf("%d %d\n",x1/2,x2/2);}}return 0;}
0 0
- acm杭电acm5974 A Simple Math Problem 数论(最大公约数最小公约数,解方程x )
- HDU 5974 A Simple Math Problem(转化解方程)
- HDU 5974 A Simple Math Problem (数论+解方程组)
- ACM-ICPC Asia Regional Contest HDU 5974 A Simple Math Problem(数论)
- A Simple Math Problem(最小公倍数最大公约数变形应用)
- HDU 5974 A Simple Math Problem(数论)
- hdu 5974 A Simple Math Problem (数论思路)
- HDU 5974 A Simple Math Problem(数论)
- [HDU](5974)A Simple Math Problem ---数论
- HDU 5974 A Simple Math Problem(数学解方程)——2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
- 2016ACM/ICPC亚洲区大连站 1006 Detachment 1004 A Simple Math Problem(求逆元)(数论)
- 杭电ACM HDU 2522 A simple problem
- HDU 5974 A Simple Math Problem 数论 gcd
- A Simple Math Problem
- A Simple Math Problem
- A Simple Math Problem
- A Simple Math Problem
- A Simple Math Problem
- 数据库笔记 6
- 门面模式(Facade 外观模式,对象结构型模式)
- iptables语法
- Matlab批量实现图片文件格式转换
- 安卓GridView不均分解决过程
- acm杭电acm5974 A Simple Math Problem 数论(最大公约数最小公约数,解方程x )
- Android开源库V
- 多线程系列提高(7)--同步工具类
- MySQL之聚合与排序
- 优酷来疯直播安卓控件,支持flv、rtmp,支持添加视频特效等等
- win10 uwp 读写XML
- 循环神经网络(RNN, Recurrent Neural Networks)介绍
- Linux中LCD设备驱动 framebuffer(帧缓冲)
- Java中的Stack