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. js 原生手写AJAX

    前言:最近在学习react,在练习中模拟一个button通过AJAX向后台发送POST请求,懒得引入AXIOS,就顺便练习了js原生ajax. 正文: 注:我忽略了IE6及以下版本 submit(){ ...

  2. Laravel5.5 数据库迁移:创建表与修改表

    数据库迁移是数据库的版本管理,要使用数据库迁移,需要在.env文件中连接好数据库(不多说).laravel本身已经存在user表和password_resets表的迁移了,因此,执行 php arti ...

  3. canvas学习笔记之2d画布基础的实现

    一. Canvas是啥 < canvas > 是一个可以使用脚本(通常是js)来绘图的HTML元素 < canvas > 最早由Apple引入WebKit,用于Mac OS X ...

  4. blfs(systemd版本)学习笔记-编译安装配置dhcpcd

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! dhcpcd项目地址:http://www.linuxfromscratch.org/blfs/view/stable-syst ...

  5. Java Filter防止sql注入攻击

    原理,过滤所有请求中含有非法的字符,例如:, & < select delete 等关键字,黑客可以利用这些字符进行注入攻击,原理是后台实现使用拼接字符串,案例:某个网站的登入验证的SQ ...

  6. JavaScript面向对象编程指南(三) 函数

    第3章 函数 3.1 什么是函数 函数:本质是一种代码的分组形式.函数的声明如下: <script type="text/javascript"> /*函数的声明组成: ...

  7. Python入门基础之函数、切片

    Python之函数 Python不但能非常灵活地定义函数,而且本身内置了很多有用的函数,可以直接调用. Python之调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需 ...

  8. Android 弹性布局 FlexboxLayout了解一下

    原文链接:https://mp.weixin.qq.com/s/Mi3cK7xujmEMI_rc51-r4g RelativeLayout.LinearLayout等常用布局相信大家早已耳熟能详,今天 ...

  9. Java 一些知识点总结

    本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺(阅读本文需要有一定的Java基础).本文的 ...

  10. Orchard详解--第二篇 启动

    Orchard Framework作为框架它与类库最大的区别就是框架是将一系列零散的组件组合在一起形成一个整体,接下来就对Orchard Framework如何分析Orchard如何将相关组件结合在一 ...