[CODEVS 1288]埃及分数

时间:2023-03-10 01:24:15
[CODEVS 1288]埃及分数

Description

在古埃及,人们使用单位分数的和(形如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),编程计算最好的表达方式。

Input

a b

Output

若干个数,自小到大排列,依次是单位分数的分母。

Sample Input

19 45

Sample Output

5 6 18

题解

迭代的入门经典题...然而我现在才做...

我们将深度迭代,搜索。

由于要使最小的分数最大,所以我们要边搜的时候记录当前状况下的最优解。

加个$A*$。

我们用估价函数算出枚举分母的范围区间。

注意开$int$会爆。

 #include<map>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
#define RE register
#define IL inline
using namespace std;
const LL INF=~0u>>; LL keep[],ans[];
LL gcd(LL a,LL b) {return b ? gcd(b,a%b):a;} LL a,b,lim; void Dfs(LL cen,LL x,LL y)
{
if (cen>lim) return;
LL g=gcd(x,y);x/=g;y/=g;
if (x==&&y>keep[cen-])
{
keep[cen]=y;
if (keep[cen]<ans[cen]) memcpy(ans,keep,sizeof(ans));
return;
}
LL s=y/x;
if (keep[cen-]>=s) s=keep[cen-]+;
LL t=y*(lim-cen+)/x;
for (LL i=s;i<=t;i++)
{
keep[cen]=i;
Dfs(cen+,i*x-y,y*i);
}
} int main()
{
scanf("%lld%lld",&a,&b);
for (;;lim++)
{
ans[lim]=INF;
Dfs(,a,b);
if (ans[]>&&ans[]<INF)
{
for (RE LL i=;i<=lim;i++) printf("%lld ",ans[i]);
return ;
}
}
return ;
}