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 ;
}

最新文章

  1. java.lang.NullPointerException的可能原因及处理
  2. R语言读取本地文件注意事项
  3. Android中如何控制元素的显示隐藏?
  4. sybase 收集常用sql语句
  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
  6. Spring 源码解读 推荐流程
  7. IE6、7下inline-block不起作用
  8. 0520 python
  9. 实战nginx 基础知识总结(一)1.1
  10. 你不明白 String 类别
  11. Java多线程基础——线程间通信
  12. Chapter 2 Open Book——4
  13. OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
  14. 老大哥在看着你!我国部署超2000万个AI监控系统
  15. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)
  16. wxpython 支持python语法高亮的自定义文本框控件的代码
  17. &lt;Android基础&gt;(三) UI开发 Part 1
  18. Django REST framework 第七章 Schemas &amp; client libraries
  19. ububtu下安装配置搜狗输入法
  20. Hibernate日期映射类型

热门文章

  1. python基础学习(十三)函数进阶
  2. css控制文字自动换行
  3. 下载使用前端开发工具sublime,并汉化
  4. 2018-08-24 中文代码之Spring Boot对H2数据库简单查询
  5. 【20190219】CSS-知识点整理:float、em、浏览器的渲染过程
  6. Genymotion安卓模拟器和VirtualBox虚拟机安装、配置、测试
  7. Python&#160;利用Python操作excel表格之openyxl介绍Part2
  8. C# 实现中国象棋【棋盘,棋子】
  9. 小程序实践(三):九宫格实现及item跳转
  10. Android为TV端助力 android 在5.0以后不允许使用隐式Intent方式来启动Service