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: thereare k restrictedintegersthatshouldnotbeusedasadenominator. For example, if we can’t use 2..6, the best solution is:
2/3 = 1/7 + 1/9 + 1/10 + 1/12 + 1/14 + 1/15 + 1/18 + 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. 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: 1. Watch out for floating-point errors if you use double to store intermediate result. We didn’t use double. 2. Watch out for arithmetic overflows if you use integers to store intermediate result. We carefully checked our programs for that.

 Sample Input

5
2 3 0
19 45 0
2 3 1 2
5 121 0
5 121 1 33
 

 Sample Ouput

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

题解:IDA*算法,标注的是困难版本,其实和lrj在之前讲的没什么区别。只要是这个算法,主框架就都是一样的。我在之前的博客里提到过是否需要d==maxd的判断,由于这个题估价函数的特点,这句话是需要的。估价函数很好理解,如果在接下来的搜索中,即便分数的大小都是目前最大的数也无法达到目标,必然就要剪枝。

 #include <bits/stdc++.h>

 using namespace std;
typedef long long LL; const int maxn = + ;
int k, maxd;
bool canuse[maxn];
LL a, b;
LL ans[maxn],v[maxn]; LL gcd(LL a, LL b) {
return b == ? a : gcd(b, a%b);
} LL get_first(LL a, LL b) {
return (b - ) / a + ;
} bool better(int d) {
for (int i = d; i >= ; i--) {
if (v[i] != ans[i]) {
return ans[i] == - || 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 (v[d]<= && !canuse[v[d]]) return false;
if (better(d)) memcpy(ans, v, (d+) * sizeof(LL));
return true;
} bool ok = false;
for (LL i = max(from, get_first(a, b));; i++) {
if(i<= && !canuse[i]) continue;
if (b*(maxd + - d) <= i * a) break;
v[d] = i;
LL b2 = b * i,a2 = a * i - b;
LL g = gcd(a2, b2);
if (dfs(d + , i + , a2 / g, b2 / g)) ok = true;
}
return ok;
} int main()
{
int iCase = ;
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld%d", &a, &b, &k);
memset(canuse, true, sizeof(canuse));
int x;
while(k--){
scanf("%d", &x);
canuse[x] = false;
}
printf("Case %d: ", iCase++);
for (maxd = ;; maxd++) {
memset(ans, -, sizeof(ans));
if (dfs(,get_first(a,b), a, b)) break;
}
printf("%lld/%lld=1/%lld", a, b, ans[]);
for (int i = ; i <= maxd; i++) {
printf("+1/%lld", ans[i]);
}
printf("\n");
}
return ;
}

UVA12558-Efyptian Fractions(HARD version)(迭代加深搜索)的更多相关文章

  1. UVA12558 Egyptian Fractions (HARD version) (埃及分数,迭代加深搜索)

    UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个 ...

  2. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  3. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  4. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  5. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  6. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  7. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  8. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  9. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

随机推荐

  1. Spring 中事务控制的API介绍

    1.PlatformTransactionManager Spring所有事务代理类都是基于PlatformTransactionManager接口的实现. 此接口是spring的事务管理器,它里面提 ...

  2. linux /mac 下 go环境变量配置

    安装了go语言之后,还要设置路径,如果不设置路径,则执行 go 的时候会提示 go: command not found,提示的意思是没有这个命令行.这个是因为还没有设置PATH路径. 设置路径的方式 ...

  3. Hibernate入门(四)---------一级缓存

    Hibernate一级缓存 Hibernate的一级缓存就是指Session缓存,Session缓存是一块内存空间,用来存放相互管理的java对象,在使用Hibernate查询对象的时候,首先会使用对 ...

  4. elementUI vue v-model的修饰符

    v-model的修饰符 v-model.lazy 只有在input输入框发生一个blur时才触发 v-model.trim 将用户输入的前后的空格去掉 v-model.number 将用户输入的字符串 ...

  5. CentOS7防火墙firewalld设置

    添加80端口  重启后永久生效 firewall-cmd --zone=public --add-port=80/tcp --permanent   查看防火墙状态 systemctl status ...

  6. 深圳共创力“研发管理&知识管理”高端研讨交流会在深圳举办!

    2017/4/8,由深圳市共创力企业管理咨询公司举办的“研发管理&知识管理”高端研讨会在深圳市南山区圣淘沙国际酒店(翡翠店)隆重召开.此次研讨会由共创力总经理.首席顾问杨学明先生主持.研讨会先 ...

  7. 机器学习之EM算法(五)

    摘要 EM算法全称为Expectation Maximization Algorithm,既最大期望算法.它是一种迭代的算法,用于含有隐变量的概率参数模型的最大似然估计和极大后验概率估计.EM算法经常 ...

  8. 如何在HTTP客户端与服务器端之间保持状态(转)

    HTTP协议与状态保持 HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有必要纪录彼此过去的行为,每一次请求之间都 ...

  9. raid1 raid2 raid5 raid6 raid10的优缺点和做各自raid需要几块硬盘

    Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...

  10. NoSQL与MongoDB介绍

    写在前面 本文是由一次演讲整理出来的,文中大部分资料来源于网络,感谢Wikipedia,Google和MongoDB官网.文中使用的MongoDB版本为1.2.4. What is NoSQL NoS ...