HDU - 5974 - A Simple Math Problem 【 数论 】题解

电玩女神 2021-07-25 00:18 464阅读 0赞

目录

      • 1.题目
      • 2.代码

1.题目

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*104),b(1≤b≤109),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

2.代码

  1. //c*i2-a*i+b=0
  2. //a.b.c已知二元一次方程组求根 小的那个为i 大的为j
  3. #include<iostream>
  4. #include<cstdio>
  5. #include<cmath>
  6. typedef long long ll;
  7. using namespace std;
  8. long long gcd(long long a,long long b)
  9. {
  10. if(b==0) return a;
  11. else return gcd(b,a%b);
  12. }
  13. int main()
  14. {
  15. long long a,b;
  16. while(cin>>a>>b)
  17. {
  18. ll c=gcd(a,b);
  19. ll d=a*a-4*c*b;
  20. if(d<0)
  21. {
  22. cout<<"No Solution"<<endl;
  23. continue;
  24. }
  25. ll i=(a-sqrt(d))/2/c;
  26. ll j=a/c-i;
  27. ll x=i*c;
  28. ll y=j*c;
  29. if(x*y/c==b)
  30. cout<<x<<' '<<y<<endl;
  31. else
  32. cout<<"No Solution"<<endl;
  33. }
  34. return 0;
  35. }

发表评论

表情:
评论列表 (有 0 条评论,464人围观)

还没有评论,来说两句吧...

相关阅读