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{ ...
随机推荐
- 利用autoit自动关闭指定标题窗口
最近使用PL/SQL Developer 比较两个数据库数据差异,因部分表上没有主键,PL/SQL 就会弹出一个确认框提示某某表没有主键.因为有很多表没有主键,就不停的弹出确认窗口,得不停的点击 ...
- 返回本机的mac物理路径
/// <summary> /// 返回本机的mac物理路径 /// </summary> /// <return ...
- js网页返回页面顶部的小方法
咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部 本文就记录下js实现代码: 1.在html页面body添加dom元素 <img src="toTop ...
- C#开发客户端、JAVA和tomcat开发服务端
hessian入门,Hello和文件上传范例,C#客户端+Java Tomcat后台 2.Hello范例1)后台--定义Java接口:package org.migle.hessian; public ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- PC-HTML5-搜索框
代码如下: <input type="text" placeholder="输入 回车搜索" autofocus x-webkit-speech>很 ...
- asp.net断点续传技术---下载(转)
断点续传的原理 在了解HTTP断点续传的原理之前,先来说说HTTP协议,HTTP协议是一种基于tcp的简单协议,分为请求和回复两种.请求协议是由客户机(浏览器)向服务器(WEB SERVER)提交请求 ...
- 创建一个简单的配置android编译环境的脚本
由于有多个Android项目,每个项目配置编译环境时选项都不同,所以尝试写一个sh脚本来完成这个功能. 首先进入bin文件夹,新建一个文件enbuild $ cd ~/bin $ touch ...
- 提示框的优化之自定义Toast组件之(一)Toast组件的布局实现
开发步骤: 在res下layout下创建一个Toast的布局资源文件toast_customer.xml 在最外层布局组件中为该布局添加android:id属性 //toast_custo ...
- Python3.5入门学习记录-函数
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...