hdu 5974 LCM+求方程

来源:互联网 发布:前端自学软件 编辑:程序博客网 时间:2024/06/05 20:32


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 8798 10780
Sample Output
No Solution308 490
题意:给出a,b的值,求X+Y=a,且满足LCM(X,Y)=b的两个X,Y

题解:由于没有让求最小或最大的a,b,那么肯定是需要根据确定的表达式求出唯一的解

lcm(x,y)=x*y/gcd(x,y);

设k=gcd(x,y);

x=i*k;

y=j*k;

其中i和j是互质的

b=lcm(x,y)=x*y/k 

变为i*j*k=b

由x+y=a得(i+j)*k=a

因为i和j互质,所以i*j和i+j互质

证明:

假设a+b与a*b不互质,则它们有共同的一个因子m.
由m是a*b因子推得m是a或b或a和b的因子,又ab互质,所以只是a或b的因子.
m是a或b的因子又是a+b的因子可推出m是a和b的因子,与ab互质矛盾.
所以a+b与a*b互质

所以k=gcd(a,b)

联立i+j和i*j求解二次方程,解得相应的整数解

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<vector>#include<stack>#include<queue>#include<algorithm>#include<sstream>#define inf 0x3f3f3f3f#define ll long longusing namespace std;ll x1,x2;ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}bool judge(ll k1,ll k2){    ll det=k1*k1-4*k2;    if(det<0)        return false;    ll t=sqrt(det);    if(t*t<det)        return false;    x1=k1+t;    x2=k1-t;    //cout<<x1<<" "<<x2<<endl;    if(x1%2==1)        x1=inf;    else        x1=x1/2;    if(x2%2==1)        x2=inf;    else        x2=x2/2;    //cout<<x1<<" "<<x2<<endl;    return true;}int main(){    int a,b;    while(cin>>a>>b)    {        ll k=gcd(a,b);        ll k1=a/k;        ll k2=b/k;        //cout<<k1<<" "<<k2<<endl;        bool flag=judge(k1,k2);        if(flag==false||x1==inf&&x2==inf)            cout<<"No Solution"<<endl;        else        {            ll x=min(x1,x2);            cout<<min(x,(k1-x))*k<<" "<<max(x,(k1-x))*k<<endl;        }    }    return 0;}



0 0
原创粉丝点击