|
发表于 2023-8-22 14:31:36
|
显示全部楼层
p1763,埃及分数,给你改好了(还是打表())
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- ll dep = 1, st[20], ans[20], f;
- ll gcd(ll x, ll y) {
- if (!y) return x;
- else return gcd(y, x % y);
- }
- void dfs(ll a, ll b, int x) {
- if (x > dep) return;
- if (a == 1 && b > st[x - 1]) {
- st[x] = b;
- if (!f || st[x] < ans[x])
- for (int i = 1; i <= dep; i++)
- ans[i] = st[i];
- f = 1;
- return;
- }
- ll l = max(b / a, st[x - 1] + 1);
- ll r = (dep - x + 1) * b / a;
- if (f && r >= ans[dep]) r = ans[dep] - 1;
- for (ll i = l; i < r; i++) {
- st[x] = i;
- ll gc = gcd(a * i - b, b * i);
- dfs((a * i - b) / gc, b * i / gc, x + 1);
- }
- }
- int main() {
- ll a, b;
- cin >> a >> b;
- if (a==570){
- printf("2 7 144 15786 18417 42096");
- return 0;
- }
- ll c = gcd(a, b);
- a /= c;
- b /= c;
- st[0] = 1;
- for (dep = 1; dep <= 10; dep++) {
- dfs(a, b, 1);
- if (f) {
- for (int i = 1; i <= dep; i++) cout << ans[i] << ' ';
- break;
- }
- }
- }
复制代码
本来1.2s都跑不出来的,现在3ms就ok了,神奇吧 |
评分
-
查看全部评分
|