题意就是给你一个a,b。并且有x + y = a 且 lcm(x,y) = b, 让你求x,y;
大致思路就是根据规律推出公式:
设g = gcd(a,b)
那么有g * K1 = x, g * K2 = y, 且 k1 和 k2 互质;
由K1 和 K2 互质可得 K1 * K2 和 K1 + K2 也互质。
又lcm(x,y) * gcd(x,y) = x * y 可得 lcm(x,y) / gcd(x,y) = K1 * K2 即:K1 * k2 = b / g;
又 x + y = a 可得 K1 + K2 = a / g;
由以上加粗部分可得:gcd(a,b) = g = gcd(x,y);
lcm(x * y) * gcd(x,y) = b * gcd(x,y)推出 x*y = b * g;
又 x + y = a;
所以我们可以得出一元二次方程:;
代码如下:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define ll long long
using namespace std;
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b,a%b) ;
}
/*
6 8
798 10780
Sample Output
No Solution
308 490
*/
int main()
{
ll a,b;
while(~scanf("%lld %lld",&a,&b))
{
ll g = gcd(a,b);
ll temp = a * a - 4 * b * g;
if(temp<0)
{
printf("No Solution\n");
continue;
}
ll temp1 =(ll)sqrt(1.0*temp);
if(temp1 * temp1!=temp ||(a+temp1)%2)
{
printf("No Solution\n");
continue;
}
ll X=(temp1+a)/2;
ll Y=a-X;
if(X>Y)
{
swap(X,Y);
}
printf("%lld %lld\n",X,Y);
}
return 0;
}