1. 程式人生 > >2016ACM/ICPC亞洲區大連站-重現賽 d A Simple Math Problem

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;
}