题意:求x,y满足x+y=a,lcm(x, y)= b。(a <= 20000, b <= 1000000000)有十二万组样例。
思路:这道题现场赛数据很水能用O(n)的复杂度爆过去。后来加了数据就过不了了。只能用O(1)的复杂度。
首先知道 x+y=a,lcm(x, y)= b。令g = gcd(x, y),、那么x = i * g, y = j * g (i,j互素,因为如果i,j不互素那么gcd(x,y)就不等于g了,而是等于g乘以i和j的最大公约数了,矛盾),i * g + j * g = a, i * j * g = b. (i + j) * g = a, i * j * g = b,因为i,j互质,所以i + j和i*j互质,故gcd(a,b) = g = gcd(x, y).
s所以就有 x + y = a, x * y = b * gcd(a, b),解一个二元一次方程组,即为答案。
#include<cstdio>
#include<set>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100 + 20;
const double EPS = 1e-5;
const int mod = 1e8 + 7;
typedef unsigned long long ull;
typedef long long ll;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, -1, 1};
int a, b;
int gcd(int x, int y){
return !y ? x : gcd(y, x % y);
}
int main(){
while(scanf("%d%d", &a, &b) == 2){
int g = gcd(a, b);
ll det = a * a - 4LL * g * b;
ll d = sqrt(det);
if(d * d == det){
ll c = a - d;
if(c % 2 == 0) {
printf("%lld %lld\n", c / 2, a - c / 2);
continue;
}
}
puts("No Solution");
}
}