HDU 5974 A Simple Math Problem (数学)

A Simple Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 396    Accepted Submission(s): 159


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

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5981  5980  5979  5978  5977 
这道题最重要的地方是知道GCD(x,y)=GCD(a,b);然后解一元二次方程就可以了。
为什么 GCD(x,y)=GCD(a,b)呢,渣渣不知道,看了别人的利用反证法证明的过程

设c = gcd(x, y);

那么可以表达出x和y了,就是x = i * c; y = j * c;

因为i和j互质,所以(i + j) 和 i * j 互质。

假设他们不互质,那么设那个数是x,有,i + j = m * x;  i * j = k * x;

那么有i和j都能整除x,(由第一条可以得到了),这和i和j互质矛盾。

所以gcd(a, b) = c了


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
__int64 GCD(__int64 a,__int64 b)
{
	return b?GCD(b,a%b):a; 
}
int main()
{
	__int64 x,x1,x2,y1,y2,y,i,j,a,b;
	while(scanf("%I64d%I64d",&a,&b)!=EOF)
	{		
			__int64 num=GCD(a,b);
			b=b*num;
			__int64 ans=a*a-4*b;
			if(ans<0)
			{
				printf("No Solution\n");
			}
			else
			{
				if((__int64)sqrt(ans)*(__int64)sqrt(ans)!=ans)
				{
					printf("No Solution\n");
				}
				else
				{
					ans=(__int64)sqrt(ans);
					y1=(a+ans)/2;
					x1=a-y1;
					y2=(a-ans)/2;
					x2=a-y2;
					if((a+ans)&1)
					{
						if((a-ans)&1)
						{
							printf("No Solution\n");
						}
						else
						{
							printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
						}
					}
					else
					{
						if((a-ans)&1)
						{
							if(min(x1,y1)<0)
							{
								printf("No Solution\n"); 
							}
							else
							{
								printf("%I64d %I64d\n",min(x1,y1),max(x1,y1));
							}
						}
						else
						{
							if(min(x1,y1)<0)
							{
								 printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
							}
							else
							{
								if(min(x2,y2)<min(x1,y1))
								{
									
									printf("%I64d %I64d\n",min(x2,y2),max(x2,y2));
								}
								else
								{
									printf("%I64d %I64d\n",min(x1,y1),max(x1,y1));
								}
							}
						}
					}
				}
			}
		}
	
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值