二分求条。玄关
查看原帖
二分求条。玄关
902351
Little_x_starTYJ楼主2024/10/13 12:11
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int a, b, ans;
		cin >> a >> b;
		if (b == 0) {
			cout << a << endl;
			continue;
		}
		if (a < b) {
			cout << 0 << endl;
			continue;
		}
		int l = 1, r = 1e14;
		while (l < r) {
			int mid = l + r >> 1;
//			cout << l << ' ' << r << endl;
			if (a / mid <= b) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		ans = l;
		l = 1, r = 1e14;
		while (l < r) {
			int mid = l + r + 1 >> 1;
//			cout << l << ' ' << mid << ' ' << r << endl;
			if (a / mid < b) {
				r = mid - 1;
			} else {
				if (a / mid > b)
					l = mid + 1;
				else
					l = mid;
			}
		}
		ans = l - ans + 1;
		cout << ans << endl;
	}
	return 0;
}
2024/10/13 12:11
加载中...