2016ACM/ICPC亚洲区大连站-重现赛 d A Simple Math Problem

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*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

此题存在以下难点
证明1
i,j互质则i+j,ij互质
(如果 i, j 互质, i+j 和 i
j 不互质,那么 i+j 和 ij 存在公因数 T ,那么由题意可知 ,T必定为 i 或 j 的因数,同时 T 还为 i + j 的因数,那么假设 i % t == 0, ( i + j ) % t == 0, j 必定为 t 的一个倍数,满足不了i j 互质,故猜想成立。)
证明2
如果i,j互质那么i
k=A,j*k=B那么gcd(A,B)=k(易证,如果k不是AB的gcd那么gcd m一定大于k(因为k已经是AB的一个公约数了,最大的公约数m肯定比k大)那么就要从i,j中提取一部分给k使之变为m,但i,j互质)
下面就简单了
A = X+Y , B= GMD(X,Y) 求 x 最小时的 X Y ,无解输出 No Solition

然后先去看本题 A = X+Y , B = GMD(X,Y) 那么有已知的最小公倍数和最大公约数关系可知

B * GCD(X,Y) = X*Y

看到 X*Y已知,X+Y已知,求X Y,很不由自主的就想到了韦达定理。。

那么根据韦达定理 (设 gcd(x,y) = k)

X+Y = A

X-Y = sqrt( AA - 4k*B)

所以整个问题的难点之一就在于 如何把 k 变成已知量
ki=X
k
j=Y
i, j互质
k(i+j)=A
k(ij)=B
根据1 i+j和i
j互质
根据2 gcd(A,B)=k;


#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long int 
ll gcd(ll a,ll b)
{
	if(a%b)
        return gcd(b,a%b);
    return b;
}
ll max(ll a, ll b)
{
	if(a>b)
	return a;
	else
	return b;
}
ll min(ll a, ll b)
{
	if(a>b)
	return b;
	else
	return a;
}
ll a,b;
ll x,y;
int main()
{
	while(~scanf("%lld%lld",&a,&b))
	{
        ll k=gcd(a,b);
        ll d = a*a-4*b*k;
        ll dd =(ll)sqrt(1.0*d);
        if(d<0)
        {
        	printf("No Solution\n");
        	continue;
		}
		else if((dd+a)%2!=0||dd*dd!=d)
		{
			printf("No Solution\n");
        	continue;
		}
		else
		{
			x = (a + dd) / 2;
			y = a - x;
			printf("%lld %lld\n",min(x,y),max(x,y));
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值