D - A Simple Math Problem HDU - 5974 (数论+二分)

题目链接

这一题我们很容易想到暴力,但是会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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值