HDU 5974 A Simple Math Problem 2016大连区域赛

A Simple Math Problem
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 14    Accepted Submission(s): 12


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(1a2*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



Source
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学) 

打表发现…gcd(X,Y)=gcd(a,b)
所以:
x+y=a
x*y=gcd(a,b)*b
得到一元二次方程
PS:居然没有special judge 要输出(x,y)字典序小的答案 题面也没给 醉了…

证明:

:gcd(X,Y)=d
x=X/d
y=Y/d
LCM(X,Y)=X*Y/gcd(X,Y)=x*y*d
又因为:
b=LCM(X,Y)=x*y*d
a=X+Y=(x+y)*d
gcd(a,b)=gcd( x*y*d  , (x+y)*d )
显然gcd(x,y)=1
所以gcd(a,b)=d=gcd(X,Y)

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    ll a,b;
    while(~scanf("%lld%lld",&a,&b)){
        bool haveAns=false;
        ll k=gcd(a,b)*b;
        //x^2-ax+k==0
        if(a*a<4*k){
            puts("No Solution");
            continue;
        }
        double t=sqrt(a*a-4*k);
        ll ans1=((double)a-t)/2.0;
        ll ans2=((double)a+t)/2.0;
        if(ans1*(a-ans1)==k){
            printf("%lld %lld\n",ans1,a-ans1);
            continue;
        }
        if(ans2*(a-ans2)==k){
            printf("%lld %lld\n",ans2,a-ans2);
            continue;
        }
        puts("No Solution");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值