D - Simple Math Problem
戳上方进入原题哟~
题目大意
给一个数 a,b。 让你求满足一下条件的 X,Y:
- X+Y = a
- LCM(X,Y) = b
解析
题目给出时间限制1s,这道题暴力肯定过不了了0.0。
在审题的时候,突然发现 gcd(a,b) = gcd(X,Y),个人感觉这是解题关键于是就以这个为基础开始推导:
设gcd(a,b) = gcd(X,Y) = g
则有 gk1 = X , gk2 = Y
不难看出 a = g*(k1+k2)
且 b = g * k1 * k2 ------------------------> 取 g* k1 = X为例,那么 b = X * k2 一目了然为什么 b为这个式子
故有 k1k2 = b/g , k1+k2 = a/g
那么对于第一个式子同时乘g^2,第二个式子同时乘g得到: XY= b*g , X+Y = a
由于a , b已知则不难求出 X,Y
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int BOKI = 1e6+10;
typedef long long ll;
int gcd(int a,int b){ return b?gcd(b,a%b):a;}
int main(){
int a,b;
while(cin >> a >> b){
ll g = gcd(a,b);
ll d=(a*a)-(4*b*g);
if(d < 0){
cout << "No Solution" << endl;
continue;
}
ll x=(a+sqrt(d))/2;
ll y=a-x;
if(x*y==b*g)
{
cout<<y<<" "<<x<<endl;
}
else
{
cout<<"No Solution"<<endl;
}
}
return 0;
}
END