UVa 12558 埃及分数(Egyptian Fractions (HARD version) )

题目:
相比于埃及分数,多了一个某些数字不能出现的。
说困难,实际上只要判断一下某个数是否可以用而已。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e3 + 10;

unordered_set<LL> s;
LL arr[maxn];
LL ans[maxn];

bool better(LL d) {
    for(LL i = d; i >= 0; i--) {
        if(arr[i] != ans[i]) {
            return ans[i] == -1 || arr[i] < ans[i];
        }
    }
    return false;
}

bool dfs(LL d, LL maxd, LL a, LL b, LL maxe) {
    // cout << "d: " << d << endl;
    // cout << "maxd: " << maxd << endl;
    // cout << "a: " << a << " b: " << b << endl;
    // cout << "maxe: " << maxe << endl;
    // cout << endl;
    if(d == maxd) {
        if(a == 1 && !s.count(b)) {
            arr[d] = b;
            if(better(d)) {
                for(LL i = 0; i <= d; i++) {
                    ans[i] = arr[i];
                }
            }
            return true;
        }
        return false;
    }
    LL fir = max(b / a + 1, maxe);
    //cout << fir << endl;
    bool ok = false;
    for(LL i = fir;; i++) {
        if(s.count(i)) continue;
        // IDA* 启发函数
        //cout << i << endl;
        if((maxd - d + 1) * b  <= i * a) break;
        LL t = i * b;
        LL left = a * i - b;
        LL g = __gcd(t, left);
        
        arr[d] = i;
        //cout << t << " " << left << endl;
        if (dfs(d + 1, maxd, left / g, t / g, i + 1)) ok = true;
    }
    return ok;
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    int kase = 0;
    while(T--) {
        cout << "Case " << ++kase << ": ";
        s.clear();
        LL a, b, n;
        cin >> a >> b >> n;
        while(n--) {
            LL x;
            cin >> x;
            s.insert(x);
        }
        LL maxd = 1;
        cout << a << "/" << b << "=";
        for(maxd;;maxd++) {
            //cout << endl << maxd;
            memset(ans, -1, sizeof(ans));
            if(dfs(0, maxd, a, b, b / a + 1))
            break;
        }
        for(LL i = 0; i <= maxd; i++) {
            if(i) cout << "+";
            cout << "1/" << ans[i];
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
埃及分数是一种特殊的分数表示方式,它表示为若干个分数之和,其中每个分数的分子为1,分母为正整数,且分母互不相同。例如,6/7可以表示为1/2 + 1/3 + 1/42。 贪心算法可以用来求解埃及分数,具体实现如下: 1. 输入一个正分数x,初始化一个空数组fractions用于存放埃及分数。 2. 从最大的分母开始,不断尝试将x表示为1/denominator的形式,直到x变为0或者分母为1为止。 3. 对于每个分母,计算出当前能够表示的最大的1/denominator的分数,将该分数加入fractions数组中,并将x减去该分数。 4. 如果x已经为0,返回fractions数组作为结果;否则,重新从最大的分母开始尝试表示x,重复步骤3和4,直到x变为0为止。 以下是具体的C语言实现: ```c #include <stdio.h> int main() { double x; scanf("%lf", &x); // 输入待转换的分数 int denominator = 1; // 分母从1开始 int fractions[100], count = 0; // 存放埃及分数的数组和计数器 while (x > 0) // 当x不为0时继续循环 { int numerator = (int) (denominator * x + 1); // 计算当前能够表示的最大分数的分子 fractions[count++] = numerator / denominator; // 将该分数加入fractions数组 x -= 1.0 * fractions[count-1] / denominator; // 减去该分数 denominator = numerator % denominator; // 更新分母 } printf("Egyptian fractions: "); for (int i = 0; i < count; i++) printf("1/%d + ", fractions[i]); printf("\b\b \n"); // 输出埃及分数 return 0; } ``` 运行该程序,输入一个待转换的分数,即可得到其对应的埃及分数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值