hdu 5974 A Simple Math Problem

来源:互联网 发布:g92内螺纹编程实例解释 编辑:程序博客网 时间:2024/05/29 17:56
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 Solution

308 490

ac代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int gcd(int a,int b){    return b?gcd(b,a%b):a;}int main(){    int x,y;    while(cin>>x>>y)    {        int d=gcd(x,y);        int k=x/d;        int h=y/d;        int flag=0;        int l=k*k-4*h;        double p;        if(l<0||(sqrt(l)*sqrt(l)!=l))        flag=1;        else        {            p=(k+sqrt(l))/2.0;            if(int(p)!=p)            flag=1;        }        if(flag==1)        cout<<"No Solution"<<endl;        else if((int)p<=(k-(int)p))        cout<<(int)p*d<<" "<<(k-(int)p)*d<<endl;        else cout<<(k-(int)p)*d<<" "<<(int)p*d<<endl;    }    return 0;}

原创粉丝点击