This commit is contained in:
e2hang
2025-09-11 15:53:33 +08:00
parent 6839d2ea13
commit 265b8af720
7 changed files with 384 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
using namespace std;
vector<int> best_solution; // <20><>ǰ<EFBFBD><C7B0><EFBFBD>Ž<EFBFBD>
int best_max_den = INT_MAX; // <20><>ǰ<EFBFBD><C7B0><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ
// gcd <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
// DFS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void dfs(vector<int>& path, int step, int max_depth,
long long num, long long den, int a, int b, int start) {
if(step > max_depth) return;
if(num * b == den * a) {
int cur_max = *max_element(path.begin(), path.end());
if(best_solution.empty() || cur_max < best_max_den) {
best_solution = path;
best_max_den = cur_max;
}
return;
}
// <20><><EFBFBD>޼<EFBFBD>֦
long long i_start = max((long long)start, (den + num - 1) / num); // ceil(den/num)
for(long long i = i_start; i <= 15000; i++) {
path.push_back(i);
long long new_num = num * i + den;
long long new_den = den * i;
long long g = gcd(new_num, new_den);
new_num /= g;
new_den /= g;
if(new_num * b <= new_den * a) { // <20><>֦
dfs(path, step + 1, max_depth, new_num, new_den, a, b, i + 1);
}
path.pop_back();
}
}
// IDS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<int> egyptian_fraction(int a, int b) {
best_solution.clear();
best_max_den = INT_MAX;
for(int depth = 1; depth <= 15000; depth++) { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȿɵ<C8BF>
vector<int> path;
dfs(path, 0, depth, 0, 1, a, b, 1);
if(!best_solution.empty()) break; // <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>̳<EFBFBD><CCB3>Ƚ<EFBFBD>
}
return best_solution;
}
int main() {
int a, b;
cin >> a >> b;
vector<int> res = egyptian_fraction(a, b);
for(int x : res) cout << x << " ";
cout << endl;
return 0;
}