D - A Simple Math Problem HDU - 5974数论—欧几里得

@数论之欧几里得

gcd(an,bn)=ngcd(a,b);
这道题可以用到的是:
条件:a=x+y;b=x
y;
结论:gcd(x,y)==gcd(a,b);

题目:D - 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*104),b(1≤b≤109),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

Sponsor
因为a,b已知,直接求出temp=gcd(x,y)=gcd(a,b)
这道题目用完这个东西数论知识之后就可以得到x*(a-x)==temp*b;对这个等式求x有解,用dot的正负来判断是否有解,因为需要得到两个正整数,用dot的开根是否是一个整数,因为a,dot同奇同偶,所以a,sqrt(dot)也是同奇同偶关系,不需要判断是否奇数除以二得到0.5的问题啦。但是我今天上午wa啦一上午,原因:sqrt运算完成之后没有进行取整数就在乘起来判断是否sqrt得到的是否是一个整数,这样的话直接必然等于,必错
先是一个错代码:

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
    if(b==0)
    return a;
    return gcd(b,a%b);
}
int is(ll dot,ll a)
{
    if(sqrt(dot)*sqrt(dot)!=dot)return 0;
//    if((dot+a)%2==1)
//    return 0;
    return 1;
}
int main()
{
    long long int a,b,x,y;
    while(~scanf("%lld%lld",&a,&b))
    {
        ll temp=gcd(a,b);//-x*x+a*x=temp*b;
        ll dot=a*a-4*temp*b;
        if(dot<0||!is(dot,a))
        printf("No Solution\n");
        else
        {
            x=(a-sqrt(dot))/2;
            y=(a+sqrt(dot))/2;
            printf("%lld %lld\n",x,y);
        }
    }

    return 0;
}

再来个对的:

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
    if(b==0)
    return a;
    return gcd(b,a%b);
}
int is(ll dot,ll a)
{
    if((int)sqrt(dot)*(int)sqrt(dot)!=dot||(dot+a)%2==1)return 0;   return 1;
}
int main()
{
    long long int a,b,x,y;
    while(~scanf("%lld%lld",&a,&b))
    {
        ll temp=gcd(a,b);//-x*x+a*x=temp*b;
        ll dot=a*a-4*temp*b;
        if(dot>=0&&is(dot,a))
        {
            x=(a-sqrt(dot))/2;
            y=(a+sqrt(dot))/2;
            printf("%lld %lld\n",x,y);
        }else
        printf("No Solution\n");
    }
    return 0;
}

求一个数是否是一个平方数,麻烦记住对sqrt()取整,切记切记。。。。。。。。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值