A Simple Math Problem HDU - 5974



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


还是大字看着舒服啊,可能是我眼睛不行了,现在迷之老年人生活态度,等我先去泡杯茶。

好的我泡茶回来了,刚学刷茶杯路过隔壁实验室的时候发现一个小姐姐正在看琅琊榜……我也想看琅琊榜……(QAQ)
好的回到这道题,一开始我想到了X,Y的最大公约数就是a,b的最大公约数,但是之后解方程我不会解了,十分忏愧,用的二分来查找解:
l=1;r=a/2+1;
mid=(l+r)/2;
if(mid*(a-mid)>b) r=mid-1;
if(mid*(a-mid)<b) l=mid+1;
我自认为对的很,但是不知道为何过不了,可能是我二分写的也太菜了吧,希望哪个有识之士能帮我实现这个方法
下面是中规中矩解方程的代码,我现在连这种方程都不会解了,也是废了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b){  
    return b==0?a:gcd(b,a%b);  
} 
int main()
{
    ll a,b;
    while(scanf("%lld%lld",&a,&b)!=EOF)
    {
        int ok=1;
        ll c=gcd(a,b);
        ll x,y;
        a=a/c,b=b/c;
        ll g1=a*a-4*b;
        if(g1<0)ok=0;
        else
        {
            ll g2=sqrt(g1);
            if(g2*g2!=g1)ok=0;
            else
            {
                if((a-g2)%2!=0||(a+g2)%2!=0)  
                ok=0;
                else
                {
                x=(g2+a)/2*c;
                y=(a-g2)/2*c;
                if(x>y)
                {  int temp=x;  x=y;  y=temp;  }     
                }
            }
        }
        if(ok)
        printf("%d %d\n",x,y);
        else
        printf("No Solution\n");
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/xiachongyubing/p/7384117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值