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. Java中关于Map的使用(HashMap、ConcurrentHashMap)

    在日常开发中Map可能是Java集合框架中最常用的一个类了,当我们常规使用HashMap时可能会经常看到以下这种代码: Map<Integer, String> hashMap = new ...

  2. python面向对象学习(七)单例

    目录 1. 单例设计模式 单例设计模式的应用场景 2. __new__ 方法 3. Python 中的单例 只执行一次初始化工作 1. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常 ...

  3. Java web.xml笔记

    Javaweb项目中, web.xml文件其中的各种设置, 就是简单的标注 <?xml version="1.0" encoding="UTF-8"?&g ...

  4. Oracle+mybatis实现对数据的简单增删改查

    第一步:--创建一个表空间:名字叫 mybatis,建在D盘下的date文件夹下: 第二步:创建用户,名字叫  lisi  ,密码为  :123456 第三步:给用户授权: 第四步:我们在    li ...

  5. jfinal框架学习过程

    刚刚学习jfinal,通过一天左右的时间大体上理解了这个框架的用法,我对他的理解是JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功 ...

  6. Django之模板

    Django模板系统 官方文档 常用语法 Django模板中只需要记两种特殊符号: {{  }}和 {% %} {{ }}表示变量,在模板渲染的时候替换成值,{% %}表示逻辑相关的操作. 变量 {{ ...

  7. 前端学习 之 Bootstrap(二)

    一.代码 内联代码:用<code>包裹,但是需要用<和>表示尖括号. 键盘输入:用<kbd>包裹表示键盘输入的内容. 多行代码:用<pre>包裹多行代码 ...

  8. K8S flannel

    kubernetes网络通信方式有: 容器间的通信 : pod内的容器通信 通过(lo)设备 Pod之间的通信 :pod IP <-----> pod IP ,K8S 要求所有的 pod ...

  9. c#无边框窗体移动

    [DllImport("user32.dll")]//拖动无窗体的控件 public static extern bool ReleaseCapture(); [DllImport ...

  10. loadrunner 脚本优化-集合点设置

    脚本优化-集合点设置 by:授客 QQ:1033553122 添加集合点(Insert->Rendezvous) 当一个集合点被插入,VuGen往Vuser脚本中插入一个lr_rendezvou ...