Simple Math Problem HDU - 5974

Simple Math Problem

 题目链接:HDU - 5974

题意:已知a, b,求x, y, 使得x, y,满足x+y=a,lcm(x, y)=b(lcm表示最小公倍数);

联立上述两式得:x^2-a*x+b*gcd(x, y)=0;

令gcd(x, y)=k;则有x'*k+y'*k=a  \Rightarrow  (x'+y')*k=a  \Rightarrow  x'+y'=a/k;

而且:x*y/k=b  \Rightarrow  x'*k*y'*k/k=b  \Rightarrow  x'*y'=b/k;

因为x'与y'互质,所以x'+y' 与 x'*y' 互质;所以a/k 与 b/k互质;所以gcd(a, b)=k=gcd(x, y);

综上所述:x^2-a*x+b*gcd(a, b)=0;

a, b已知,就是求解一元二次方程的正整数解;

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
	if(a<b) swap(a, b);
	return b==0?a:gcd(b, a%b);
}
int main(){
	int a, b;
	while(~scanf("%d%d", &a, &b)){
		int c=gcd(a, b);
		int p=a*a-4*b*c;
		if(p<0) printf("No Solution\n");
		else{
			int q=(int)sqrt(p);
			if(q*q!=p) printf("No Solution\n");
			else{
				int x=(a-q)/2, y=a-x;
				printf("%d %d\n", x, y);
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sirius_han/article/details/81148334