UVA_埃及分数(Hard Version) UVA 12588
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的更多相关文章
- UVA12558 Egyptian Fractions (HARD version)(埃及分数)
传送门 题目大意 给出一个真分数 a/b,要求出几个互不相同的埃及分数(从大到小),使得它们之和为 a/b (埃及分数意思是分子为1的分数,详见百度百科) 如果有多组解,则分数数量少的优先 如果分数数 ...
- 华为OJ平台——将真分数分解为埃及分数
题目描述: 分子为1的分数称为埃及分数.现输入一个真分数(分子比分母小的分数,叫做真分数),请将该分数分解为埃及分数.如:8/11 = 1/2+1/5+1/55+1/110. 输入: 输入一个真分数, ...
- 埃及分数&&The Rotation Game&&骑士精神——IDA*
IDA*:非常好用的搜索,可以解决很多深度浅,但是规模大的搜索问题. 估价函数设计思路:观察一步最多能向答案靠近多少. 埃及分数 题目大意: 给出一个分数,由分子a 和分母b 构成,现在要你分解成一系 ...
- 埃及分数问题_迭代加深搜索_C++
一.题目背景 http://codevs.cn/problem/1288/ 给出一个真分数,求用最少的1/a形式的分数表示出这个真分数,在数量相同的情况下保证最小的分数最大,且每个分数不同. 如 19 ...
- Vijos 1308 埃及分数(迭代加深搜索)
题意: 输入a.b, 求a/b 可以由多少个埃及分数组成. 埃及分数是形如1/a , a是自然数的分数. 如2/3 = 1/2 + 1/6, 但埃及分数中不允许有相同的 ,如不可以2/3 = 1/3 ...
- codevs1288 埃及分数(IDA*)
1288 埃及分数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的 ...
- JDOJ 1770 埃及分数
JDOJ 1770: 埃及分数 https://neooj.com/oldoj/problem.php?id=1770 Description 分子均为1的分数叫做埃及分数,因为古代埃及人在进行分数运 ...
- 一本通例题埃及分数—题解&&深搜的剪枝技巧总结
一.简述: 众所周知,深搜(深度优先搜索)的时间复杂度在不加任何优化的情况下是非常慢的,一般都是指数级别的时间复杂度,在题目严格的时间限制下难以通过.所以大多数搜索算法都需要优化.形象地看,搜索的优化 ...
- 埃及分数问题(带乐观估计函数的迭代加深搜索算法-IDA*)
#10022. 「一本通 1.3 练习 1」埃及分数 [题目描述] 在古埃及,人们使用单位分数的和(形如 $\dfrac{1}{a}$ 的,$a$ 是自然数)表示一切有理数.如:$\dfrac{ ...
随机推荐
- JavaScript实现网页右下角弹出窗口代码
<script language="JavaScript"><!--var no = 50;var speed = 1;var ns4up = (document ...
- 解决Struts2.2.20版本的标签不支持style属性的问题
我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, ...
- 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack
上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...
- UITextView 输入长度限制
//还可以输入的长度. - (void)textViewDidChange:(UITextView *)textView { UITextRange *markRange = textView.mar ...
- Git的一些用法(建立新的branch)
建立新的branch和查看全部的branch(kk的代码是基于现有的branch) 切换到branch kk: 当然我们也能够在android studio里操作: 注意切换的时候代码会丢失,必须先c ...
- 3. QT窗体间值的传递(续)
一.前言 上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1.信号和槽的方式传值:2.主窗体中将传出值设置为publi ...
- 一道movfuscator混淆过的简单逆向
月赛中出了道经过movfuscator混淆的逆向题目,记录一下过程.跑起来发现需要用户输入长度为20的字符串,我尝试着输入了几次都是直接退出了,没有任何提示.用IDA打开,题目里面几乎全是mo ...
- XMLHttpRequest发送请求
*open(method,url,async) *send(string)//在使用get请求的时候,是没有主体的,所有的信息都会拼在url当中,所以使用send的时候括号里的string可以为空!如 ...
- Flex 扩展combobox 实现复选功能
前段时间,老大召唤,给个任务,研究一下flex的combobox控件 实现复选功能(flex自带的combobox控件不可以复选),小兵能力有限,哪里可以扩展呀,网上看了别人写的,发现总是有点瑕疵(关 ...
- 1203.4——循环语句 之 for
for循环的一般形式为:for(表达式1; 表达式2; 表达式3){ 语句块} 它的执行过程如下:1) 先求解表达式1. 2) 求解表达式2,若其值为真(非0),则执行循环体,否则结束循环. 3 ...