描述

在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数。如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的。对于一个分数a/b,表示方法有很多种,但是哪种最好呢?首先,加数少的比加数多的好,其次,加数个数相同的,最小的分数越大越好。

如:19/45=1/3 + 1/12 + 1/180
19/45=1/3 + 1/15 + 1/45
19/45=1/3 + 1/18 + 1/30,
19/45=1/4 + 1/6 + 1/180
19/45=1/5 + 1/6 + 1/18.
最好的是最后一种,因为1/18比1/180,1/45,1/30,1/180都大。

给出a,b(0<a<b<1000),编程计算最好的表达方式。

输入:a b
输出:若干个数,自小到大排列,依次是单位分数的分母。

样例1

样例输入1

19 45 

样例输出1

5 6 18

限制

各个测试点1s

(来自https://vijos.org/p/1308


这道题很明显使用搜索

(1)深搜,不知道深度

(2)广搜,保存的数据量可能会过大

(3)迭代加深,剪剪枝时间应该可以过

那就用迭代加深

不过还有一些问题:

(1)从哪开始枚举分母

  前面剩下分数的倒数取整和前一个分母+1的最小值

  第二个很好理解,至于第一个:

  假设前面剩下了a/b,则用[b/a]作分母,新分数可能会比原分数大一点点

 所以可以做开始的条件

(2)结束为(最大深度-当前深度+1)*(b/a)取整

Code

 /**
*Vijos.org &codevs.cn
*Problem#1308 Problem#1288
*Accepted Accepted
*Time:528ms 741ms
*Memort:564k 256k
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long
#define ll long long
#define _max(a,b) (a>b)?(a):(b)
using namespace std;
/**
*定义分数类
*/
typedef bool boolean;
typedef class MyClass{
public:
int s;
int m;
MyClass():s(),m(){}
MyClass(int s,int m):s(s),m(m){}
#undef int
MyClass operator -(MyClass another){
MyClass result;
int g=getCommon(this->m,another.s);
result.m=this->m*another.m/g;
result.s=this->s*another.m/g-this->m/g*another.s;
int g1=getCommon(result.m,result.s);
result.m/=g1;
result.s/=g1;
return result;
}
boolean empty(){
return (m==);
}
boolean operator <(MyClass another){
if(another.empty()) return true;
if(this->empty()) return false;
if(this->s==another.s) return this->m>another.m;
return (this->s*1.0/this->m)<(another.s*1.0/another.m);
}
boolean operator <(double another){
if(this->empty()) return false;
return (this->s*1.0/this->m)<another;
}
inline int getrInt(){
// if(s==0) return -1;
return (int)(this->m/this->s);
}
boolean isWorkable(){
if(this->empty()) return false;
return (m%s==);
}
void operator <<(istream &in){
in>>s>>m;
}
private:
int getCommon(ll a,ll b){
if(b==) return a;
return getCommon(b,a%b);
}
}MyClass;
MyClass maxx;
int results[];
int list[];
int found = ; /**
*迭代加深
*/
void search(int depthLimit,int now,MyClass fs){
if(fs<) return ;
if(now>depthLimit) return ;
if(fs.isWorkable()&&(fs.m>list[now-])&&(found==||maxx<fs)){
maxx=fs;
results[depthLimit]=fs.m/fs.s;
memcpy(results,list,sizeof(int)*depthLimit);
found=depthLimit;
return ;
}
for(int i=_max(fs.getrInt(),list[now-]+);i<=(depthLimit-now+)*fs.getrInt();i++){
list[now]=i;
search(depthLimit,now+,fs-MyClass(,i));
}
}
int main(){
MyClass q;
q<<cin;
// cout<<q.getrInt();
int i=;
list[]=;
while(found==){
search(i,,q);
i++;
}
for(int i=;i<=found;i++){
printf("%d ",results[i]);
}
}

迭代加深

Vijos 1308 埃及分数 - 迭代加深的更多相关文章

  1. Vijos 1308 埃及分数(迭代加深搜索)

    题意: 输入a.b, 求a/b 可以由多少个埃及分数组成. 埃及分数是形如1/a , a是自然数的分数. 如2/3 = 1/2 + 1/6, 但埃及分数中不允许有相同的 ,如不可以2/3 = 1/3 ...

  2. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  3. codevs 1288 埃及分数 (迭代加深搜索)

    题目大意:给你一个分数$a/b$,把它拆解成$\sum_{i=1}^{n}1/ai$的形式,必须保证$ai$互不相同的情况下,尽量保证n最小,其次保证分母最大的分数的分母最小 什么鬼玄学题!!! 因为 ...

  4. 埃及分数问题_迭代加深搜索_C++

    一.题目背景 http://codevs.cn/problem/1288/ 给出一个真分数,求用最少的1/a形式的分数表示出这个真分数,在数量相同的情况下保证最小的分数最大,且每个分数不同. 如 19 ...

  5. vijos1308 埃及分数(迭代加深搜索)

    题目链接:点击打开链接 题目描写叙述: 在古埃及.人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数.如:2/3=1/2+1/6,但不同意2/3=1/3+1/3,由于加数中有同样的.对于 ...

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

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

  7. [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)

    传送门 迭代加深搜索是必须的,先枚举加数个数 然后搜索分母 这里有一个强大的剪枝,就是确定分母的范围 #include <cstdio> #include <cstring> ...

  8. 埃及分数问题(带乐观估计函数的迭代加深搜索算法-IDA*)

    #10022. 「一本通 1.3 练习 1」埃及分数 [题目描述] 在古埃及,人们使用单位分数的和(形如 $\dfrac{1}{a}​$​​ 的,$a$ 是自然数)表示一切有理数.如:$\dfrac{ ...

  9. 埃及分数(codevs 1288)

    题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的. 对于一 ...

随机推荐

  1. 解读经典面试题for循环console.log

    for (var i = 1; i <= 5; i++) { setTimeout(function () { console.log(i) },1000)} 会打印出5个6,这是why 因为 ...

  2. Servlet交互与JSP

    主要内容介绍 数据共享与页面跳转 1. 为什么要有跳转: Servlet需要跳转到其它Servlet中,因为我们需要职责分明,不同Servlet来完成不同的功能 Servlet跳转到JSP中,Serv ...

  3. shell基础:环境变量

    子shell是在父shell中打开的shell. 使用pstree查看进程树. $调用环境变量 set查看所有变量内容, env查询环境变量 只是临时改变

  4. 函数max()优化

    函数max的优化 用途:查询最后支付时间-优化max函数 语句: select max(payment_date)from payment 执行计划:

  5. DataGridView常用属性和方法

    DataGridView常用属性: 只读属性设定    datagridview.ReadOnly = True 行自动追加    datagridview.AllowUserToAddRows = ...

  6. Rpgmakermv(7) Chronus.js说明与简要翻译

    插件地址:https://github.com/triacontane/RPGMakerMV/blob/master/Chronus.js 日语版 ゲーム内で時刻と天候の概念を表現できるプラグインです ...

  7. Lua用一维数组存储一个n阶方阵,输出这个方阵的正对角线上的数的和与反对角线上的数的和的差的绝对值。

    arr = {, , , , , , , , -} function diagonalDifference(arr) dimesion = math.sqrt(#arr) arr1 = {} sum1 ...

  8. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

  9. jQuery事件--keypress([[data],fn])和trigger(type,[data])

    keypress([[data],fn]) 概述 当键盘或按钮被按下时,发生 keypress 事件 keypress 事件与 keydown 事件类似.当按钮被按下时,会发生该事件.它发生在当前获得 ...

  10. Linux 进程间通讯

    一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...