萌新求助极短Stern-Brocot树!!悬关!
  • 板块P5179 Fraction
  • 楼主Link_Cut_Y
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/5/1 10:25
  • 上次更新2023/10/23 17:03:36
查看原帖
萌新求助极短Stern-Brocot树!!悬关!
519384
Link_Cut_Y楼主2023/5/1 10:25

直接在 Stern-Brocot树上二分 T 了。考虑提前计算接下来连续几次走左/右儿子,结果WA了。路过大佬救救!!

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

using PII = pair<int, int>;
PII ans;
int A, B, C, D;

void solve(int a = 0, int b = 1, int c = 1, int d = 0) {
	int x = a + c, y = b + d;
	double now = (double)x / y;
	double L = (double)A / B, R = (double)C / D;
	if (now > L && now < R) {
		ans = {x, y}; return;
	}
	if (now <= L) {
		int t = (int)(A * y - B * x) / (B * c - A * d);
		solve(x + c * t, y + d * t, c, d);
	}
	else {
		int t = (int)(C * y - D * x) / (D * a - C * b);
		solve(a, b, x + a * t, y + b * t);
	}
}
int main() {
	while (scanf("%d%d%d%d", &A, &B, &C, &D) != EOF) {
		solve();
		printf("%d/%d\n", ans.first, ans.second);
	}
	return 0;
}
2023/5/1 10:25
加载中...