12558 - Egyptian Fractions (HARD version)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
typedef long long LL;
const int maxn=10010;
int maxd,t,tt;
set<LL> sk;
LL ans[maxn],v[maxn];
LL gcd(LL a,LL b){
    return b?gcd(b, a%b):a;
}
LL get_first(LL a, LL b)
{
    return b/a + 1;
}
bool better(int d){
    for(int i=d;i>=0;--i)
        if(v[i]!=ans[i]) return ans[i]==-1||v[i]<ans[i];
    return false;
}
bool dfs(int d,LL from,LL a,LL b){
    if(d==maxd){
        if(b%a) return false;
        v[d]=b/a;
        if(sk.count(b/a)) return false;
        if(better(d)) memcpy(ans,v,sizeof(LL)*(d+1));
        return true;
    }
    bool ok =false;
    for(LL i=max(from,get_first(a,b)); ; ++i){
        if(b*(maxd+1-d)<=i*a) break;
        if(sk.count(i)) continue;
        v[d]=i;
        LL b2=b*i;
        LL a2=a*i-b;
        LL g=gcd(a2,b2);
        if(dfs(d+1,i+1,a2/g,b2/g)) ok=true;
    }
    return ok;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        LL a,b,k,sk0;
        sk.clear();
        scanf("%lld%lld%lld",&a,&b,&k);
        while(k--) scanf("%lld",&sk0),sk.insert(sk0);
        for(maxd=0;;++maxd)
        {
            memset(ans,-1,sizeof(ans));
            if(dfs(0,get_first(a,b),a,b)) break;
        }
        printf("Case %d: %lld/%lld=",++tt,a,b);
        for(int i=0;i<=maxd;++i)
        {
            if(i) printf("+");
            printf("1/%lld",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

时间: 2024-08-29 23:37:02

12558 - Egyptian Fractions (HARD version)的相关文章

UVA 12558 Egyptian Fractions (HARD version)

题意: 经典的埃及分数问题,即给出一个真分数,求出用个数最少的单位分数来表示这个分数.如果有多种方案,要让每个分数尽量的大,即分母尽量的小.会有K个禁止使用的单位分数. 分析:    IDA*算法.当按照分母递增的顺序排列时, 如果当前考虑的分数为1/e,剩下的maxd - d+1层都是1/e,但仍然到不了目标的a/b的时候,就剪枝. 代码: #include <iostream>#include <cstdio>#include <cstring>#include &

12558 - Egyptian Fractions (HARD version)(IDA*算法)

IDA*算法,迭代加深搜索和A*算法的结合 . 迭代加深搜索适用于那些没有明显深度上限的题目,将深度从小到大枚举,直到找到最优解 ,减小了深搜的盲目性 . A*算法需要一个乐观估价函数,在这个函数里寻找一个代价最小的点去搜索,所以时间复杂度都浪费在这个上面了 . 代码如下: #include<bits/stdc++.h> using namespace std; typedef long long ll; int T,kase=0; ll v[10000+10],ans[10000+10],a

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

UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个数和分母的上限,只用爆搜绝对TLE.故只能用迭代加深搜索. #include<cstdio> #include<cstring> #include<set> using namespace std; typedef long long ll; int num,T,t,k;

UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)

题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; # define LL long long int num[5],a,b,k; LL ans[10000],v[10000]; LL gcd(LL a,LL b) { return (b==0)?a:gcd(b,a%b)

【习题 7-7 UVA-12558】Egyptian Fractions (HARD version)

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大量maxdep 在dfs里面传剩余的要凑的分子.分母 以及上一次枚举的值是多少. 然后找到最小的k,满足1/k<=分子/分母 然后从max(k,last+1)开始枚举. ->剪枝就是剩余的全都用这个最大的分数.如果都不行就肯定不行了. 二分找这个k. 不能用的数字就直接跳过就行. [代码] /* 1.Shoud it use long long ? 2.Have you ever test severa

UVA12558-Efyptian Fractions(HARD version)(迭代加深搜索)

Problem UVA12558-Efyptian Fractions(HARD version) Accept:187  Submit:3183 Time Limit: 3000 mSec  Problem Description Given a fraction a/b, write it as a sum of different Egyptian fraction. For example, 2/3 = 1/2 + 1/6. Thereisonerestrictionthough: th

UVA_埃及分数(Hard Version) UVA 12588

Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egyptian fraction. Forexample, 2/3=1/2+1/6.There is one restriction though: there are k restricted integers that should notbe used as a denominator. For

maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet

问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: properties->Java Compiler,修改JDK版本,然后Apply

异常:Unsupported major.minor version 52.0 (Use --stacktrace to see the full trace)

异常:Unsupported major.minor version 52.0 (Use --stacktrace to see the full trace) 正在写一个功能,更新了同事提交的代码之后,出现了如下错误: 1 Fatal error during compilation org.apache.tools.ant.BuildException: java.lang.UnsupportedClassVersionError: org/elasticsearch/index/query