????????? 7-7 UVA-12558???Egyptian Fractions (HARD version)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了????????? 7-7 UVA-12558???Egyptian Fractions (HARD version)相关的知识,希望对你有一定的参考价值。

?????????you   mes   val   tor   ??????   cal   logs   open   a*   

???????????? ????????????,?????????:)
????????????


?????????????????????

????????????


?????????????????????
???????????????maxdep
???dfs??????????????????????????????????????????
???????????????????????????????????????
?????????????????????k?????????1/k<=??????/??????
?????????max(k,last+1)???????????????
->??????????????????????????????????????????????????????????????????????????????????????????
???????????????k.
??????????????????????????????????????????

????????????

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
    8.whatch out the detail input require
*/
/*
    ???????????????????????????????????????!!!
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e3;

bool bo[N+10];
int a,b,k,maxdep;
vector<ll> v,ans;

ll getidx(int a,int b){
    //1/i <= a/b ??? i??????
    //b<=a*i
    ll l= 1,r = 1e8;
    ll temp = -1;
    while (l <= r){
        int m = (l+r)>>1;
        if (1LL*a*m>=b){
            temp = m;
            r = m - 1;
        }else l = m + 1;
    }
    return temp;
}

bool Greater(vector<ll> v,vector <ll> ans){
    if ((int)ans.size()==0) return true;
    for (int i = (int)v.size()-1;i>=0;i--)
        if (v[i]!=ans[i]){
                if (v[i]>ans[i]) return false;else return true;
            }
    return false;
}

bool dfs(int dep,int a,int b,ll last){
    if (dep==maxdep){
        if (a==1 && b>last){
            if (b<=1000 && bo[b]) return false;
            v.push_back(b);
            if (Greater(v,ans)) ans = v;
            v.pop_back();
            return true;
        }
        return false;
    }
    ll idx = getidx(a,b);
    ll ma = max(last+1,idx);
    ll delta = maxdep-dep+1;
    //delta/ma<a/b
    bool ok = false;

    for (ll i = ma; ;i++)
        {
            if (i<=1000 && bo[i]) continue;
            if (delta*b<a*i) break;
            //a/b - 1/i
            v.push_back(i);
            ll fenzi = a*i-b,fenmu = b*i;
            ll temp = __gcd(fenzi,fenmu);
            fenzi/=temp,fenmu/=temp;
            if (dfs(dep+1,fenzi,fenmu,i)) ok = true;
            v.pop_back();
        }
    return ok;
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin >> T;
    int kase = 0;
    while(T--){
        memset(bo,0,sizeof bo);
        cin >> a >> b >> k;
        while (k--){
            int x;
            cin >> x;
            bo[x] = 1;
        }
        ans.clear();
        v.clear();
        for (maxdep = 1;;maxdep++){
            if (dfs(1,a,b,1)){
                cout <<"Case "<<++kase<<": "<<a<<"/"<<b<<"=1/"<<ans[0];
                for (int i = 1;i <(int) ans.size();i++)
                    cout << "+1/"<<ans[i];
                break;
            }
        }
        cout << endl;
    }
    return 0;
}

以上是关于????????? 7-7 UVA-12558???Egyptian Fractions (HARD version)的主要内容,如果未能解决你的问题,请参考以下文章

UVA12558 Egyptian Fractions (HARD version) (埃及分数)

UVA12558 Egyptian Fractions (HARD version)(埃及分数)

数据结构7. 排序

北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重

每次值更改时如何拆分列表? [复制]

学习记录:快速幂