当前位置:网站首页>洛谷P1763 埃及分数
洛谷P1763 埃及分数
2022-08-11 04:00:00 【CLH_W】
题目描述
来源:BIO 1997 Round 1 Question 3
在古埃及,人们使用单位分数的和(形如 \dfrac{1}{a}
a
1
的,aa 是自然数)表示一切有理数。如:\dfrac{2}{3} = \dfrac{1}{2} + \dfrac{1}{6}
3
2
2
1
+
6
1
,但不允许 \dfrac{2}{3} = \dfrac{1}{3} + \dfrac{1}{3}
3
2
3
1
+
3
1
,因为加数中有相同的。对于一个分数 \dfrac{a}{b}
b
a
,表示方法有很多种,但是哪种最好呢?首先,加数少的比加数多的好,其次,加数个数相同的,最小的分数越大越好。如:
\begin{aligned} \frac{19}{45} &= \frac{1}{3} + \frac{1}{12} + \frac{1}{180}\ \frac{19}{45} &= \frac{1}{3} + \frac{1}{15} + \frac{1}{45}\ \frac{19}{45} &= \frac{1}{3} + \frac{1}{18} + \frac{1}{30}\ \frac{19}{45} &= \frac{1}{4} + \frac{1}{6} + \frac{1}{180}\ \frac{19}{45} &= \frac{1}{5} + \frac{1}{6} + \frac{1}{18}\ \end{aligned}
45
19
45
19
45
19
45
19
45
19
=
3
1
+
12
1
+
180
1
=
3
1
+
15
1
+
45
1
=
3
1
+
18
1
+
30
1
=
4
1
+
6
1
+
180
1
=
5
1
+
6
1
+
18
1
最好的是最后一种,因为 \dfrac{1}{18}
18
1
比 \dfrac{1}{180}, \dfrac{1}{45}, \dfrac{1}{30}
180
1
,
45
1
,
30
1
都大。
注意,可能有多个最优解。如:
\begin{aligned} \frac{59}{211} &= \frac{1}{4} + \frac{1}{36} + \frac{1}{633} + \frac{1}{3798}\ \frac{59}{211} &= \frac{1}{6} + \frac{1}{9} + \frac{1}{633} + \frac{1}{3798}\ \end{aligned}
211
59
211
59
=
4
1
+
36
1
+
633
1
+
3798
1
=
6
1
+
9
1
+
633
1
+
3798
1
由于方法一与方法二中,最小的分数相同,因此二者均是最优解。
给出 a,ba,b,编程计算最好的表达方式。保证最优解满足:最小的分数 \ge \cfrac{1}{10^7}≥
10
7
1
。
输入格式
一行两个整数,分别为 aa 和 bb 的值。
输出格式
输出若干个数,自小到大排列,依次是单位分数的分母。
输入输出样例
输入 #1复制
19 45
输出 #1复制
5 6 18
说明/提示
1 \lt a \lt b \lt 10001<a<b<1000
上代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL long long
#define MAXN 1010
#define max(x,y) (x > y ? x : y)
LL a, b, depth;
bool flag;
LL temp[MAXN], ans[MAXN]; // 分别记录当前答案与最终答案。
LL gcd (LL x, LL y) {
return (!y ? x : gcd (y, x % y));
} // 求最大公约数。
void dfs (LL newa, LL newb, LL step) {
if (step + 1 == depth) {
if (newa == 1) {
temp[step + 1] = newb;
flag = true;
if (temp[depth] < ans[depth] || !ans[depth]) {
memcpy (ans, temp, sizeof (temp));
}
}
return ;
}
LL first = newb % newa ? newb / newa + 1 : newb / newa;
for (LL i = max (temp[step] + 1, first); i <= ceil (newb / newa) * (depth - step); i++) {
temp[step + 1] = i;
LL nxta, nxtb, g;
nxta = newa * i - newb;
nxtb = newb * i;
g = gcd (nxta, nxtb);
dfs (nxta / g, nxtb / g, step + 1);
}
} // 搜索主体。
int main (void) {
scanf ("%d %d", &a, &b);
LL g = gcd (a, b);
temp[0] = 1;
for (depth = 1; ; depth++) {
dfs (a / g, b / g, 0);
if (flag) {
break ; }
}
for (int i = 1; i <= depth; i++) {
printf ("%d ", ans[i]);
}
return 0;
}
边栏推荐
- 移动端地图开发选择哪家?
- 多串口RS485工业网关BL110
- En-us is an invalid culture error solution when Docker links sqlserver
- 【FPGA】SDRAM
- 【FPGA】名词缩写
- 【FPGA】SDRAM
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
- The FTP error code list
- 快速使用UE4制作”大场景游戏“
- Get Qt installation information: including installation directory and various macro addresses
猜你喜欢
Kubernetes集群搭建Zabbix监控平台
多串口RS485工业网关BL110
北湖区燕泉街道开展“戴头盔·保安全”送头盔活动
获取Qt的安装信息:包括安装目录及各种宏地址
一文读懂 高性能可预期数据中心网络
机器学习中什么是集成学习?
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
Read the article, high-performance and predictable data center network
【FPGA】day22-SPI协议回环
Qnet Weak Network Test Tool Operation Guide
随机推荐
LeetCode刷题第17天之《3 无重复字符的最长子串》
Detailed explanation of VIT source code
Get the length of the linked list
The solution to the height collapse problem
How to rebuild after pathman_config and pathman_config_params are deleted?
一文读懂 高性能可预期数据中心网络
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
A simple JVM tuning, learn to write it on your resume
uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
[FPGA] Design Ideas - I2C Protocol
What problems should we pay attention to when building a programmatic trading system?
(转)JVM中那些区域会发生OOM?
Read the article, high-performance and predictable data center network
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
【深度学习】基于卷积神经网络的天气识别训练
【FPGA】day22-SPI protocol loopback
拼多多店铺营业执照相关问题
MYSQLg advanced ------ return table
“顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?