这一题我们很容易想到暴力,但是会TLE。
通过打表观察到 gcd(X,Y)==gcd(a,b) 。 并且X*(a-X) 在对称轴的一侧满足单调递增 。我们假设存在正确答案,将LCM中的 X*Y/GCD 中的GCD设为定值,那么我们就可以使用二分将时间复杂度压低。
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <functional>
using namespace std;
#define F cout<<"bug\n";
typedef long long ll;
const int N = 4e5 + 10;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
int lowbit(int x) { return (x & (-x)); }
int ls(int u) { return u << 1; }
int rs(int u) { return u << 1 | 1; }
int GCD;
int gcd(int a,int b) {
return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a,int b) {
return a / gcd(a, b) * b;
}
ll f(int a,int b) {
return (ll)a * (ll)b / GCD;
}
int main() {
int a, b;
while (scanf("%d %d",&a,&b)!=EOF) {
GCD = gcd(a, b);
int l = 1, r = a / 2; if (a & 1)r++;
while (l < r) {
int mid = l + r >> 1;
int tmp = f(mid, a - mid);
if (tmp < b)l = mid + 1;
else r = mid;
}
if (lcm(l, a - l) == b) {
printf("%d %d\n", l, a - l);
}
else printf("No Solution\n");
}
return 0;
}