UVA12558-Efyptian Fractions(HARD version)(迭代加深搜索)
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
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)(迭代加深搜索)的更多相关文章
- UVA12558 Egyptian Fractions (HARD version) (埃及分数,迭代加深搜索)
UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个 ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
随机推荐
- 慕课网maven多环境配置
profile 下面的节点,是把profile 标签内容复制几份,并且需要把<activation> 去掉.在idea右侧 maven button 中会出现选择 节点. 接着打包命令和打 ...
- Hibernate入门(九)级联删除
Hibernate级联删除 上一篇文章学习了级联保存和更新,这个级联删除应该很好理解的.一样的道理,删除一方,同时删除有关联的一方. https://www.cnblogs.com/deepSleep ...
- Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】
本篇文章中,我将跟大家一起实现 Article 的新增.编辑和删除功能,仔细解读每一段代码,相信本篇文章看完,你就能够 get Laravel 使用之道. RESTful 资源控制器 资源控制器是 L ...
- ListView子项点击无反应的解决办法
在使用ListView控件的过程中,当子项包括Button或者CheckBoX等控件时,直接点击子项无反应,分析发现原来是Button,CheckBoX等控件会优先获取焦点,那么子项点击的焦点就被上述 ...
- vue从入门到进阶:简介(一)
前言 用了这么久的vue了,但是一直没有时间写个系列文章,现在抽一定时间总结下vue的知识点. 首先,Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript ...
- mysql随笔系列-1
MySQL数据库管理 本人实验所用的MySQL数据库版本:5.5.56-MariaDB MariaDB Server 操作系统:centos7.5 1.创建数据库 MariaDB [(none)]& ...
- C# 对象持久化
本文以一个简单的小例子,简述对象持久化的相关知识,仅供学习分享使用,如有不足之处,还请指正. 概述 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术. 本文介绍的是除数据库 ...
- Android studio 下的SDK Manager只显示已安装包的情况
原因是连接不上Google的更新服务器: 解决方法: 选择第三个Options: 修改Http Proxy Server: mirrors.neusoft.edu.cn Http Proxy Port ...
- Android为TV端助力 史上最简单易懂的跨进程通讯(Messenger)!
不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...
- Android为TV端助力 关于JNI的使用方法
1首先在java里面定义你需要的native方法 2打开cmd,进入doc窗口,如果是android项目就进入到你当前项目的bin目录下,在doc里面输入cd E:\workspace\Test1 也 ...