A Simple Math Problem HDU - 5974(思维+数论)

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
题意:

要我们求解xy使得
x+y=a x + y = a
lcm(x,y)=b l c m ( x , y ) = b
如果能找到一组解输出x y,否则输出“No Solution”

分析:

首先我们对二式变一下型

lcm(x,y)=b l c m ( x , y ) = b

xygcd(x,y)=b x ⋅ y g c d ( x , y ) = b

xy=bgcd(x,y) x ⋅ y = b ⋅ g c d ( x , y )
x+y=a x + y = a

x(ax)=bgcd(x,y) x ⋅ ( a − x ) = b ⋅ g c d ( x , y )

x2ax+bgcd(x,y)=0 x 2 − a x + b ⋅ g c d ( x , y ) = 0

所以题目转化成了求解这个二次方程,看似一元二次方程
但是其中还有一个大问题就是 gcdxy g c d ( x , y ) 怎么求?或者说我们可以怎么表示 gcdxy g c d ( x , y )

下面我们对 x,y x , y 进行分析:

g=gcd(x,y) g = g c d ( x , y )

则:

x=k1g x = k 1 ⋅ g

y=k2g y = k 2 ⋅ g

因为g是x,y的最大公因数那么 k1,k2 k 1 , k 2 一定是互质的数

我们代回最初的方程组中得到

{k1g+k2g=ak1gk2g=b(67) (67) { k 1 ⋅ g + k 2 ⋅ g = a k 1 ⋅ g ⋅ k 2 ⋅ g = b

                                                                                                                                                                               ↓

{k1+k2=agk1k2=bg(121) (121) { k 1 + k 2 = a g k 1 ⋅ k 2 = b g

因为 k1,k2 k 1 , k 2 是互质的,那么 k1+k2,k1k2 k 1 + k 2 , k 1 ⋅ k 2 是互质的


下面给出该结论的证明:

pq p , q 是互质的两个整数

M=p+qN=pq M = p + q , N = p q

假设M,N不是互质的

那么必有 M=aN M = a N N=aM N = a M a为整数

我们以 M=aN M = a N 为例,则原式等于 p+q=apq p + q = a p q

p=q(ap1) p = q ( a p − 1 )

pq=ap1 p q = a p − 1

因为p,q互质所以 pq p q 一定不会得到整数

但是a是整数,p是整数,则ap-1是整数

所以等式两边相矛盾故:

若p,q是互质的,那么p+q和pq也是互质的


{k1+k2=agk1k2=bg(171) (171) { k 1 + k 2 = a g k 1 ⋅ k 2 = b g

那么有了这个结论说明 ag a g bg b g 是互质的,那么g就是a,b的最大公因数

也就是 gcd(a,b)=gcd(x,y) g c d ( a , b ) = g c d ( x , y )

所以该题目转变成

求解一元二次方程 x2ax+bgcd(a,b)=0 x 2 − a x + b ⋅ g c d ( a , b ) = 0

code:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int gcd(int a,int b){
    return b ? gcd(b,a%b) : a;
}
int a,b;
int main(){
    while(scanf("%d%d",&a,&b) != EOF){
        int diat = a * a - 4 * b * gcd(a,b);
        if(diat < 0){
            printf("No Solution\n");
        }
        else{
            int p = (int)sqrt(1.0*diat);
            if(p * p != diat){
                printf("No Solution\n");
            }
            else{
                int x1 = (a + p) / 2;
                int x2 = (a - p) / 2;
                int x = min(x1,x2);
                printf("%d %d\n",x,a-x);
            }
        }
    }
    return 0;
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值