您的位置:首页 > 其它

acm杭电acm5974 A Simple Math Problem 数论(最大公约数最小公约数,解方程x )

2017-05-02 09:32 435 查看
题目地址: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


[align=left]Problem Description[/align]

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


 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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).
 

[align=left]Sample Input[/align]

6 8
798 10780

 

[align=left]Sample Output[/align]

No Solution
308 490

 

[align=left]Source[/align]
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.

证明(反证法):

设 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 互质

3、若 x ,y 的最大公约数为d,则 x/d 与 y/d 是互质的。

证明(反证法):

设 i = x/d
     j = y/d

假设 i 与 j 不互质, 则存在公约数 m,
既然 m是 i 与 j 的公约数,那么也应该是 x ,y 的公约数才对
那此时的x, y的最大公约数成了 d*m ,与前提相矛盾
所以命题成立

已知:

x + y = a 
x * 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 = a 
x * 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 = 0
double 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: