社区讨论

Unaccept 100迭代加深求助

P1763埃及分数参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mhizuks4
此快照首次捕获于
2025/11/03 18:25
4 个月前
此快照最后确认于
2025/11/03 18:25
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#define int long long

const int N = 1e7 + 5;
using namespace std;

int n, m;
int ans;
int a[N], w[N];

int gcd(int x, int y) {
    return y != 0 ? gcd(y, x % y) : x;
}
bool dfs(int cnt, int x, int y) {
	if (cnt == ans + 1) { 
		if (x == 0) {
			if (w[ans] > a[ans])
				for (int i = 1; i <= ans; i++) w[i] = a[i];
			return true;
		}
		else return false;
	}
	bool flag = false;
	for (int i = max(y / x, a[cnt - 1] + 1); i <= (ans - cnt + 1) * y / x; i++) {
		a[cnt] = i;
		int dx = x * i - y;
		int dy = y * i;
		int d = gcd(dx, dy);
		dx /= d;
		dy /= d;
		if (dfs(cnt + 1, dx, dy)) flag = true;
	}
	return flag;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n>> m;
	for (ans = 1; ; ans++) {
		w[ans] = 2e9;
		if (dfs(1, n, m) == true) break;
	}
	for (int i = 1; i <= ans; i++) cout << w[i]<< " ";
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...