Problem E
Eg[y]ptian Fractions (HARD version)
Given a fraction a/b, write it as a sum of different Egyptian fraction. For
example, 2/3=1/2+1/6.
There is one restriction though: there are k restricted integers that should not
be used as a denominator. For example, if we can't use 2~6, the best solution is:
2/3=1/7+1/8+1/9+1/12+1/14+1/18+1/24+1/28
The number of terms should be minimized, and then the large denominator should be
minimized. If there are several solutions, the second largest denominator should
be minimized etc.
Input
The first line contains the number of test cases T(T<=100). Each test case begins
with three integers a, b, k(2<=a<b<=876, 0<=k<=5, gcd(a,b)=1). The next line
contains k different positive integers not greater than 1000.
Output
For each test case, print the optimal solution, formatted as below.
Sample Input
5 2
3 0
19 45 0
2 3 1 2
5 121 0
5 121 1 33
Output for the Sample Input
Case 1: 2/3=1/2+1/6
Case 2: 19/45=1/5+1/6+1/18
Case 3: 2/3=1/3+1/4+1/12
Case 4: 5/121=1/33+1/121+1/363
Case 5: 5/121=1/45+1/55+1/1089
Extremely Important Notes
It's not difficult to see some inputs are harder than others. For example, these
inputs are very hard input for every program I have:
596/829=1/2+1/5+1/54+1/4145+1/7461+1/22383
265/743=1/3+1/44+1/2972+1/4458+1/24519
181/797=1/7+1/12+1/2391+1/3188+1/5579
616/863=1/2+1/5+1/80+1/863+1/13808+1/17260
22/811=1/60+1/100+1/2433+1/20275
732/733=1/2+1/3+1/7+1/45+1/7330+1/20524+1/26388
However, I don't want to give up this problem due to those hard inputs, so I'd
like to restrict the input to "easier" inputs only. I know that it's not a perfect
problem, but it's true that you can still have fun and learn something, isn't it?
Some tips:
Watch out for floating-point errors if you use double to store intermediate
result. We didn't use double.
Watch out for arithmetic overflows if you use integers to store intermediate
result. We carefully checked our programs for that.

解题报告

迭代加深搜索。。注意带入上一个的分母,并且保证升序就可以辣,注意不要使用受限制的分母

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
LL ban[];
int ban_num;
int maxd;
LL ans[];
LL temp[]; LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b);
} /* gs 保证升序 */
bool dfs(LL a,LL b,LL gs,int d)
{
if (d == maxd)
{
if (b % a || b <= temp[d-]) return false;
/*Check Last Num */
for(int i = ;i<ban_num;++i)
if (b == ban[i])
return false;
temp[d] = b;
int ok = ;
for(int i = d;i>=;--i)
if(ans[i] == -)
{
ok = ;
break;
}
else if(ans[i] != temp[i])
{
if (temp[i] < ans[i])
ok = ;
break;
}
if (ok)
memcpy(ans,temp,sizeof(LL) * (d+));
return true;
}
bool c = false;
LL start = max(b/a + ,gs);
for(int i = start;;++i)
{
int ok = ;
if ( a * i > b*(maxd - d + )) break;
for(int j = ;j<ban_num;++j)
if (i == ban[j])
{
ok = ;
break;
}
if (!ok) continue;
LL na = a*i - b;
LL nb = b*i;
LL gc_ = gcd(na,nb);
temp[d] = i;
if (dfs(na/gc_,nb/gc_,i+,d+))
c= true;
}
return c;
} int main(int argc,char * argv[])
{
int T2 = ;
int T;
cin >> T;
while(T--)
{
memset(ans,-,sizeof(ans));
LL a,b;
cin >> a >> b >> ban_num;
for(int i = ; i < ban_num;++i)
cin >> ban[i];
for(int i = ;;++i)
{
maxd = i;
if (dfs(a,b,,))
break;
}
printf("Case %d: %lld/%lld=",T2++,a,b);
for(int i = ;i<=maxd;++i)
i == ? printf("1/%lld",ans[i]) : printf("+1/%lld",ans[i]);
printf("\n"); }
return ;
}

UVA_埃及分数(Hard Version) UVA 12588的更多相关文章

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

    传送门 题目大意 给出一个真分数 a/b,要求出几个互不相同的埃及分数(从大到小),使得它们之和为 a/b (埃及分数意思是分子为1的分数,详见百度百科) 如果有多组解,则分数数量少的优先 如果分数数 ...

  2. 华为OJ平台——将真分数分解为埃及分数

    题目描述: 分子为1的分数称为埃及分数.现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为埃及分数.如:8/11 = 1/2+1/5+1/55+1/110. 输入: 输入一个真分数, ...

  3. 埃及分数&&The Rotation Game&&骑士精神——IDA*

    IDA*:非常好用的搜索,可以解决很多深度浅,但是规模大的搜索问题. 估价函数设计思路:观察一步最多能向答案靠近多少. 埃及分数 题目大意: 给出一个分数,由分子a 和分母b 构成,现在要你分解成一系 ...

  4. 埃及分数问题_迭代加深搜索_C++

    一.题目背景 http://codevs.cn/problem/1288/ 给出一个真分数,求用最少的1/a形式的分数表示出这个真分数,在数量相同的情况下保证最小的分数最大,且每个分数不同. 如 19 ...

  5. Vijos 1308 埃及分数(迭代加深搜索)

    题意: 输入a.b, 求a/b 可以由多少个埃及分数组成. 埃及分数是形如1/a , a是自然数的分数. 如2/3 = 1/2 + 1/6, 但埃及分数中不允许有相同的 ,如不可以2/3 = 1/3 ...

  6. codevs1288 埃及分数(IDA*)

    1288 埃及分数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的 ...

  7. JDOJ 1770 埃及分数

    JDOJ 1770: 埃及分数 https://neooj.com/oldoj/problem.php?id=1770 Description 分子均为1的分数叫做埃及分数,因为古代埃及人在进行分数运 ...

  8. 一本通例题埃及分数—题解&&深搜的剪枝技巧总结

    一.简述: 众所周知,深搜(深度优先搜索)的时间复杂度在不加任何优化的情况下是非常慢的,一般都是指数级别的时间复杂度,在题目严格的时间限制下难以通过.所以大多数搜索算法都需要优化.形象地看,搜索的优化 ...

  9. 埃及分数问题(带乐观估计函数的迭代加深搜索算法-IDA*)

    #10022. 「一本通 1.3 练习 1」埃及分数 [题目描述] 在古埃及,人们使用单位分数的和(形如 $\dfrac{1}{a}​$​​ 的,$a$ 是自然数)表示一切有理数.如:$\dfrac{ ...

随机推荐

  1. iOS AFNetworking 详解

    1. 很不错的介绍 http://m.blog.csdn.net/blog/jackljf/38736625

  2. 【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为cocos2d-x-2.1.5)    博主发现前两个系列的学习教程被严重抄袭,在这里呼吁大家请尊重开发者的劳动成果, 转载的时候请务必注 ...

  3. JUnit三分钟教程 ---- 快速起步

    JUnit三分钟教程 ---- 快速起步 摘自http://lavasoft.blog.51cto.com/62575/65625/ JUnit是个好东西,做大点的项目离不开这东西,实际中用的时候也因 ...

  4. RAID,mdadm(笔记)

    RAID: 级别:仅代表磁盘组织方式不同,没有上下之分:0: 条带    性能提升: 读,写    冗余能力(容错能力): 无    空间利用率:nS    至少2块盘1: 镜像    性能表现:写性 ...

  5. hdu 5311 Hidden String(find,substr)

    Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...

  6. Eclipse 里的 Classpath Variables M2_REPO 无法修改(maven)

      解决方法: 在C:\Documents and Settings\Administrator\.m2中放入setting.xml,并修改本地仓库为 <localRepository>D ...

  7. eclipse配置maven + 创建maven项目

        登录|注册     努力+坚持,而且还很年轻   目录(?)[+] 在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当 ...

  8. JQuery 1.3.2联动获取部门

    Sql       $(document).ready(function(){ $(".dept").bind("click", function () { v ...

  9. ASP.NET 后台下载文件方法

    void DownLoadFile(string fileName) { string filePath = Server.MapPath(fileName);//路径 //以字符流的形式下载文件 F ...

  10. Emit技术使用实例及应用思路

    System.Reflection.Emit提供了动态创建类并生成程序集的功能. 适用于.NET Framework 2.0及其以后的版本. 动态生成类在对于O/R Mapping来说有很大的作用,在 ...