Coconuts, Revisited(递推+枚举+模拟)
Description
The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening Post on October 9, 1926. The story tells about five men and a monkey who were shipwrecked on an island. They spent the first night gathering coconuts. During the night, one man woke up and decided to take his share of the coconuts. He divided them into five piles. One coconut was left over so he gave it to the monkey, then hid his share and went back to sleep.
Soon a second man woke up and did the same
thing. After dividing the coconuts into five piles, one coconut was left
over which he gave to the monkey. He then hid his share and went back
to bed. The third, fourth, and fifth man followed exactly the same
procedure. The next morning, after they all woke up, they divided the
remaining coconuts into five equal shares. This time no coconuts were
left over.
An obvious question is ``how many coconuts
did they originally gather?" There are an infinite number of answers,
but the lowest of these is 3,121. But that's not our problem here.
Suppose we turn the problem around. If we know the number of coconuts
that were gathered, what is the maximum number of persons (and one
monkey) that could have been shipwrecked if the same procedure could
occur?
Input
The input will consist of a sequence of integers, each representing the
number of coconuts gathered by a group of persons (and a monkey) that
were shipwrecked. The sequence will be followed by a negative number.
Output
For each number of coconuts, determine the largest number of persons
who could have participated in the procedure described above. Display
the results similar to the manner shown below, in the Sample Output.
There may be no solution for some of the input cases; if so, state that
observation.
Sample Input
25
30
3121
-1
Sample Output
25 coconuts, 3 people and 1 monkey
30 coconuts, no solution
3121 coconuts, 5 people and 1 monkey 题目大意:还是人和猴子分桃子,不过猴子只有一个。和UVA-10726Coco Monkey不同的是,这道题已知的是桃子数,让求可能的最多人数。
题目解析:将递推的过程反过来,枚举人数,模拟分桃子的过程。人数不会太多。 代码如下:
# include<iostream>
# include<cstdio>
# include<set>
# include<vector>
# include<fstream>
# include<cstring>
# include<algorithm>
using namespace std;
const int N=;
bool ok(int ss,int n)
{
int t=ss;
while(ss--){
if((n-)%t)
break;
n=(n-)/t*(t-);
}
if(ss==-&&(n%t==))
return true;
return false;
}
int main()
{
int n,i;
while(scanf("%d",&n))
{
if(n==-)
break;
int ans=;
for(i=;i<;++i){
if(i*(i-)>=n)
break;
if(ok(i,n)){
ans=i;
}
}
printf("%d coconuts, ",n);
if(ans>){
printf("%d people and 1 monkey\n",ans);
}else
printf("no solution\n");
}
return ;
}
Coconuts, Revisited(递推+枚举+模拟)的更多相关文章
- hdu 4517(递推枚举统计)
小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- 汉诺塔VII(递推,模拟)
汉诺塔VII Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- F(k)<(维护+枚举)\(找规律+递推+枚举)>
题意 小明有一个不降序列(f(1),f(2),f(3),--),f(k)代表在这个序列中大小是k的有f(k)个.我们规定f(n)的前12项如下图. n 1 2 3 4 5 6 7 8 9 10 11 ...
- HRBUST 1211 火车上的人数【数论解方程/模拟之枚举+递推】
火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起(包括第3站 ...
- 0x02 枚举、模拟、递推
1.TYVJ1266(这站是不是已经倒闭了啊) USACO陈年老题,对于这种开关问题啊,最多只按一次,而且第一行随便按完下面的就全确定了,类似的还有固定翻转一个长度的区间,这个也是最多翻一次的而且翻的 ...
- hdu5965扫雷 枚举+递推
题目链接 思路:枚举第一列的可能种数,然后递推即可,中途判断是否满足条件,最后再判断最后一列是否满足条件即可. #include<bits/stdc++.h> #define LL lon ...
- [数位DP]把枚举变成递推(未完)
动态规划(DP)是个很玄学的东西 数位DP实际上 就是把数字上的枚举变成按位的递推 有伪代码 for i =这一位起始值 i<=这一位终止值 dp[这一位][i]+=dp[这一位-1][i]+- ...
- 【递推】【DFS】【枚举】Gym - 101246C - Explode 'Em All
网格里放了一些石块,一个炸弹能炸开其所在的行和列.问炸光石块至少要几个炸弹. 枚举不炸开的行数,则可以得出还要炸开几列. 为了不让复杂度爆炸,需要两个优化. 先是递推预处理出f(i)表示i的二进制位中 ...
- 【NOIP模拟赛】【数学真奇妙】【递推】旅行者问题
旅行者问题 [问题描述] lahub是一个旅行者的粉丝,他想成为一个真正的旅行者,所以他计划开始一段旅行.lahub想去参观n个目的地(都在一条直道上).lahub在起点开始他的旅行.第i个目的地和起 ...
随机推荐
- web前端----Bootstrap框架补充
一.一个小知识点 1.截取长屏的操作 2.设置默认格式 3.md,sm, xs 4.空格和没有空格的选择器 二.响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等- 响应式怎 ...
- P2322 [HNOI2006]最短母串问题
P2322 [HNOI2006]最短母串问题 AC自动机+bfs 题目要求:在AC自动机建的Trie图上找到一条最短链,包含所有带结尾标记的点 因为n<12,所以我们可以用二进制保存状态:某个带 ...
- 20145332卢鑫 MSF基础应用
20145332卢鑫 MSF基础应用 实验过程 靶机的IP地址:192.168.10.160 Kali的IP地址:192.168.10.128 1.一个主动攻击 攻击XP系统的漏洞:ms08_067 ...
- bzoj 2654 tree - 二分法 - 最小生成树
给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行,每行 ...
- C#中配置文件保存的路径
http://www.codeproject.com/Tips/350010/Saving-User-Settings-in-Winform-Application 外网上找的资料 winform提供 ...
- 51NOD 1133 不重叠的线段
1133 不重叠的线段 X轴上有N条线段,每条线段有1个起点S和终点E.最多能够选出多少条互不重叠的线段.(注:起点或终点重叠,不算重叠). 例如:[1 5][2 3][3 6],可以选[2 ...
- 抽象类的继承,接口的实现,接口类型数组的使用,根据instanceof判断(返回)是否该是哪一个类型,类型的强转.
总觉得之前第2处有点问题,果然. 还需要instanceof判定一下,然后还需要把数组Animal[]转为Pet的才有方法play()~~~!
- php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式
做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...
- cannot marshal None unless allow_none is enabled
今天运行一个launch文件的时候出现了以下报错 load_parameters: unable to set parameters (last param was [/robot_state_pub ...
- Jmeter ResponseAssertion 【Ignore Status】
在Jmeter源码中AssertionGui.java中,定义了Ignore Status的作用域 /** * Checkbox to indicate whether the response sh ...