【习题 7-7 UVA-12558】Egyptian Fractions (HARD version)
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
迭代加深搜索。
枚举最大量maxdep
在dfs里面传剩余的要凑的分子、分母
以及上一次枚举的值是多少。
然后找到最小的k,满足1/k剪枝就是剩余的全都用这个最大的分数。如果都不行就肯定不行了。
二分找这个k.
不能用的数字就直接跳过就行。
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3;
bool bo[N+10];
int a,b,k,maxdep;
vector<ll> v,ans;
ll getidx(int a,int b){
//1/i <= a/b 且 i最小
//b<=a*i
ll l= 1,r = 1e8;
ll temp = -1;
while (l <= r){
int m = (l+r)>>1;
if (1LL*a*m>=b){
temp = m;
r = m - 1;
}else l = m + 1;
}
return temp;
}
bool Greater(vector<ll> v,vector <ll> ans){
if ((int)ans.size()==0) return true;
for (int i = (int)v.size()-1;i>=0;i--)
if (v[i]!=ans[i]){
if (v[i]>ans[i]) return false;else return true;
}
return false;
}
bool dfs(int dep,int a,int b,ll last){
if (dep==maxdep){
if (a==1 && b>last){
if (b<=1000 && bo[b]) return false;
v.push_back(b);
if (Greater(v,ans)) ans = v;
v.pop_back();
return true;
}
return false;
}
ll idx = getidx(a,b);
ll ma = max(last+1,idx);
ll delta = maxdep-dep+1;
//delta/ma<a/b
bool ok = false;
for (ll i = ma; ;i++)
{
if (i<=1000 && bo[i]) continue;
if (delta*b<a*i) break;
//a/b - 1/i
v.push_back(i);
ll fenzi = a*i-b,fenmu = b*i;
ll temp = __gcd(fenzi,fenmu);
fenzi/=temp,fenmu/=temp;
if (dfs(dep+1,fenzi,fenmu,i)) ok = true;
v.pop_back();
}
return ok;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
int kase = 0;
while(T--){
memset(bo,0,sizeof bo);
cin >> a >> b >> k;
while (k--){
int x;
cin >> x;
bo[x] = 1;
}
ans.clear();
v.clear();
for (maxdep = 1;;maxdep++){
if (dfs(1,a,b,1)){
cout <<"Case "<<++kase<<": "<<a<<"/"<<b<<"=1/"<<ans[0];
for (int i = 1;i <(int) ans.size();i++)
cout << "+1/"<<ans[i];
break;
}
}
cout << endl;
}
return 0;
}
【习题 7-7 UVA-12558】Egyptian Fractions (HARD version)的更多相关文章
- UVa 12558 - Egyptian Fractions (HARD version)
题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...
- UVA12558 Egyptian Fractions (HARD version) (埃及分数,迭代加深搜索)
UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个 ...
- uva12558 Egyptian Fractions (HARD version)(迭代深搜)
Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...
- 【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)
IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有 ...
- UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)
题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # i ...
- UVA12558 Egyptian Fractions (HARD version)(埃及分数)
传送门 题目大意 给出一个真分数 a/b,要求出几个互不相同的埃及分数(从大到小),使得它们之和为 a/b (埃及分数意思是分子为1的分数,详见百度百科) 如果有多组解,则分数数量少的优先 如果分数数 ...
- UVA12558-Efyptian Fractions(HARD version)(迭代加深搜索)
Problem UVA12558-Efyptian Fractions(HARD version) Accept:187 Submit:3183 Time Limit: 3000 mSec Pro ...
- UVa 10814 - Simplifying Fractions
题目大意:给一个分数,对其进行化简.因为分子.分母最大为1030,所以用要用大数. import java.io.*; import java.util.*; import java.math.*; ...
- 【例题 7-3 UVA - 10976】Fractions Again?!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] x>=y => \(\frac{1}{x}<=\frac{1}{y}\) => \(\frac{1}{x}= ...
随机推荐
- type 'simple Class' does not conform to protocol 'Example Protocol'错误
在看swift教程中"接口和扩展"这小部分. 在编写时提示"type 'simple Class' does not conform to protocol 'Examp ...
- BZOJ5105: [CodePlus2017]晨跑
[传送门:BZOJ5105] 简要题意: 给出a,b,c,求a,b,c的最小公倍数 题解: 直接搞(最近刷水题有点心态爆炸) 参考代码: #include<cstdio> #include ...
- js插件---强大的图片裁剪Cropper
js插件---强大的图片裁剪Cropper 一.总结 一句话总结:官网或者github里面的文档或者demo才是真的详细 使用的话找到图片裁剪后的base64数据,然后这个数据可下载可传递到服务器 1 ...
- mybatis :与Spring MVC 的集成
用mybatis与Spring mvc 的方式集成起来,源码在本文结尾处下载.主要有以下几个方面的配置1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dis ...
- vue 指令的用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 分享一个vue中的vue-Resource用法
//引入 <script src="//cdn.bootcss.com/vue-resource/1.2.1/vue-resource.js" type="text ...
- java httpRequest和Response
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- C# Aspose.Cells 使用汇总
Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cell ...
- Smart Pointer Guidelines
For Developers > Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...
- 在Ubuntu14.04中配置mysql远程连接教程
上一篇文章,小编带大家学会了在Ubuntu14.04中安装MySQL,没有来得及上课的小伙伴们可以戳这篇文章:如何在Ubuntu14.04中安装mysql,今天给大家分享一下,如何简单的配置MySQL ...