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. bzoj1752 [Usaco2005 qua]Til the Cows Come Home

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  2. JSP通过SmartUpload上传文件实例

    httpRequest.setCharacterEncoding("gbk"); String preName = genName.doMake();//设置文件前缀名 Strin ...

  3. hdu 5429 Geometric Progression(存个大数模板)

    Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...

  4. 用WIFI为什么连不上VPN

    因为是做服务器的,经常需要通过VPN连接到公司的服务器处理一些事件. 但最近一次在寝室通过WIFI连接VPN时却报“错误 619:不能建立到远程计算机的连接,因用于此端口的连接已关闭”.我的第一反应是 ...

  5. Ajax 获取数据代码

    无刷新获取字符串: Html网页中: <script> //定义异步对象 var xmlHttp; //封装方法 function CreateXMLHTTP() { try { xmlH ...

  6. 五分钟读懂UML类图

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  7. OpenCV——CvSeq动态结构序列

    动态结构序列CvSeq是所有OpenCV动态数据结构的基础. 分为两类: 稠密序列 稀疏序列 (1) 稠密序列都派生自CvSeq,他们用来代表可扩展的一维数组 - 向量.栈.队列和双端队列.数据间不存 ...

  8. YUI Array 之each| forEach(遍历)

    1. yui-each原码: 遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn ...

  9. js入门基础7-2 (求模-隔行变色)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. [Head First Python]3. 文件与异常:处理错误

    datafile.txt Man: Is this the right room for an argument? Other Man: I've told you once. Man: No you ...