hdu5974

A Simple Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1795    Accepted Submission(s): 524

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 8
798 10780

Sample Output

No Solution
308 490

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <string>
 5 #include <cstring>
 6 #include <cstdio>
 7 using namespace std;
 8 long long f(long long a,long long b)
 9 {
10     return (b==0)?a:f(b,a%b);
11 }
12 int main()
13 {
14     long long a,b,l,ans,t;
15     while(~scanf("%I64d%I64d",&a,&b))
16     {
17         l=f(a,b);
18         if(a*a-4*b*l<0)
19             cout<<"No Solution"<<endl;
20         else
21         {
22             ans=(a-sqrt(a*a-4*b*l))/2;
23             t=a-ans;
24             if(ans*t/f(t,ans)==b)
25                 cout<<ans<<‘ ‘<<t<<endl;
26             else
27               cout<<"No Solution"<<endl;
28         }
29     }
30     return 0;
31
32 }
时间: 2024-08-09 08:15:39

hdu5974的相关文章

HDU5974 A Simple Math Problem---数论--转化解方程

感谢:http://blog.csdn.net/mirror58229/article/details/63685884 题意:x+y=a lcm(x,y)=b  求x,y 12WCases +  b 10^9 + a 10^4 所以肯定不是枚举--肯定是公式题 接下来就是转化 x+y=a x*y/gcd(x,y)=b 令gcd(x,y)=c x=i*c,y=j*c i*c+j*c=a c*i*j=b c*(i+j)=a c*i*j=b 因为i j互质 所以gcd(a,b)=c=gcd(x,y)

HDU-5974

A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3646    Accepted Submission(s): 1140 Problem Description Given two positive integers a and b,find suitable X and Y to meet t