HDU 5974 A Simple Math Problem 数学

来源:互联网 发布:网络共享硬盘无法访问 编辑:程序博客网 时间:2024/06/05 10:36

A Simple Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2505    Accepted Submission(s): 777


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 8798 10780
 

Sample Output
No Solution308 490

一道纯的数学题。

题意:给出两个数 a,b。a是x和y的和,b是x和y的最小公倍数;

在这里根据两个数互质,则他们的和与他们的积也互质可以推导出a和b的最大公因数就是x和y的最大公因数。

然后解方程,在这里注意一下,求得的解必须是整数。

#include<stdio.h>#include<math.h>#define ll long longusing namespace std;ll gcd(ll a,ll b){   while(b!=0)    {ll c=a%b;         a=b;         b=c;    }    return a;}ll lcm(ll a,ll b){    return a/gcd(a,b)*b;}int main(){    ll a,b;    while(scanf("%lld%lld",&a,&b)!=EOF)    {        ll c=gcd(a,b);        c=b*c;        ll d=a-sqrt(a*a-4*c);        ll p=d/2;        if(a*a-4*c<0)            printf("No Solution\n");        else        {            ll z=a*a-4*c;            ll s=sqrt(z);            if(s*s==z&&p*2==d)                printf("%lld %lld\n",p,a-p);            else                printf("No Solution\n");        }    }}

原创粉丝点击