【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

迭代加深搜索。
枚举最大量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)的更多相关文章

  1. UVa 12558 - Egyptian Fractions (HARD version)

    题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...

  2. UVA12558 Egyptian Fractions (HARD version) (埃及分数,迭代加深搜索)

    UVA12558 Egyptian Fractions (HARD version) 题解 迭代加深搜索,适用于无上界的搜索.每次在一个限定范围中搜索,如果无解再进一步扩大查找范围. 本题中没有分数个 ...

  3. uva12558 Egyptian Fractions (HARD version)(迭代深搜)

    Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...

  4. 【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)

    IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有 ...

  5. UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)

    题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # i ...

  6. UVA12558 Egyptian Fractions (HARD version)(埃及分数)

    传送门 题目大意 给出一个真分数 a/b,要求出几个互不相同的埃及分数(从大到小),使得它们之和为 a/b (埃及分数意思是分子为1的分数,详见百度百科) 如果有多组解,则分数数量少的优先 如果分数数 ...

  7. UVA12558-Efyptian Fractions(HARD version)(迭代加深搜索)

    Problem UVA12558-Efyptian Fractions(HARD version) Accept:187  Submit:3183 Time Limit: 3000 mSec  Pro ...

  8. UVa 10814 - Simplifying Fractions

    题目大意:给一个分数,对其进行化简.因为分子.分母最大为1030,所以用要用大数. import java.io.*; import java.util.*; import java.math.*; ...

  9. 【例题 7-3 UVA - 10976】Fractions Again?!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] x>=y => \(\frac{1}{x}<=\frac{1}{y}\) => \(\frac{1}{x}= ...

随机推荐

  1. Hadoop的单节点集群详细启动步骤

    见,如下博客 hadoop-2.2.0.tar.gz的伪分布集群环境搭建(单节点) 很简单,不多赘述.

  2. Postfix邮件系统安装配置视频

    Postfix邮件系统安装配置视频(文字资料详见linux企业应用案例精解),全部视频分为四个部分,详情如下: http://115.com/file/be9j4dsj#postfix-1.rar h ...

  3. vmware-images

    https://virtualboxes.org/images/centos/ https://www.osboxes.org/vmware-images/

  4. Vijos——T1406 拉力赛

    https://vijos.org/p/1460 描述 车展结束后,游乐园决定举办一次盛大的山道拉力赛,平平和韵韵自然也要来参加大赛. 赛场上共有n个连通的计时点,n-1条赛道(构成了一棵树).每个计 ...

  5. es7 --- 新特性

    ES7只有2个特性: includes() 指数操作符 不使用ES7 使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断: let arr = ['react', ' ...

  6. POJ 1904 思路题

    思路: 思路题 题目诡异地给了一组可行匹配 肯定有用啊-. 就把那组可行的解 女向男连一条有向边 如果男喜欢女 男向女连一条有向边 跑一边Tarjan就行了 (这个时候 环里的都能选 "增广 ...

  7. POJ 3174 暴力枚举

    思路: 暴力枚举三个点 判一判 搞定 (x1*y1=x2*y2) x1.y1.x2.y2为他们两两的差 //By SiriusRen #include <cstdio> using nam ...

  8. ElasticSearch、Kibana Web管理

    ElasticSearch的Web管理 http://localhost:9200/ http://localhost:9200/cluster/health?pretty http://localh ...

  9. Kinect 开发 —— 常见手势识别(上)

    悬浮按钮 (Hover Button) 悬浮按钮通过将鼠标点击换成悬浮然后等待(hover-and-wait)动作,解决了不小心点击的问题.当光标位于按钮之上时,意味着用户通过将光标悬浮在按钮上一段时 ...

  10. Kinect 开发 —— Hello,Kinect

    控制台输出深度数据: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...