http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=736
A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 729 Accepted Submission(s): 177
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
题意:给你两个数a,b,让你拆成x+y=a且lcm(x,y)=b。(x,y都是整数)
求x,y
分析:
令x=ck,y=dk,则b=cdk(k=gcd(x,y))
即可得
ck+dk=a
cdk=b
解方程。
隐含条件:输出的时候小的在前,大的在后。
代码:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int maxn=100010;
const ll mo=1e9+7;
ll a,b;
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
int main()
{
while(scanf("%lld%lld",&a,&b)!=EOF)
{
ll k=gcd(a,b);
ll dt=a*a-4*k*b;
ll c=sqrt(dt);
if(dt>=0&&c*c==dt)
{
ll tmp=(a+c)/(2*k);
if((a+c)%(2*k)==0&&b%(tmp*k)==0)
{
ll cnt=b/(tmp*k);
printf("%lld %lld\n",cnt*k,tmp*k);
}
else {
tmp=(a-c)/(2*k);
if((a-c)%(2*k)==0&&b%(tmp*k)==0)
{
ll cnt=b/(tmp*k);
printf("%lld %lld\n",cnt*k,tmp*k);
}
else puts("No Solution");
}
}
else puts("No Solution");
}
return 0;
}