首页 技术 正文
技术 2022年11月16日
0 收藏 941 点赞 4,316 浏览 1251 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898