A Simple Math Problem HDU - 5974
Sample Input
6 8 798 10780
Sample Output
No Solution 308 490
题意:给出a,b两个数字,问是否存在x,y,使得x+y=a,lcm(x,y)=b。找不到输出“No Solution”
思路:大佬队友写出来的。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。
(转载自:https://www.cnblogs.com/kimsimple/p/6792395.html)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; #define PI 3.14159 #define ll long long ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int main() { ll a, b, aa, bb, cnt, x, y; while (~scanf("%lld%lld", &a, &b)) { cnt = gcd(a, b); aa = a / cnt, bb = b / cnt; if (aa * aa < 4 * bb) { printf("No Solution\n"); continue; } cnt = sqrt(aa * aa - 4 * bb); if (cnt * cnt != aa * aa - 4 * bb || (cnt + aa) % 2) { printf("No Solution\n"); continue; } x = (cnt + aa) / 2; y = aa - x; if (b % x || b % y || b / x + b / y != a) { printf("No Solution\n"); continue; } printf("%lld %lld\n", b / x, b / y); } }