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(递推+枚举+模拟)的更多相关文章

  1. hdu 4517(递推枚举统计)

    小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  2. 汉诺塔VII(递推,模拟)

    汉诺塔VII Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. 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 ...

  4. HRBUST 1211 火车上的人数【数论解方程/模拟之枚举+递推】

    火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起(包括第3站 ...

  5. 0x02 枚举、模拟、递推

    1.TYVJ1266(这站是不是已经倒闭了啊) USACO陈年老题,对于这种开关问题啊,最多只按一次,而且第一行随便按完下面的就全确定了,类似的还有固定翻转一个长度的区间,这个也是最多翻一次的而且翻的 ...

  6. hdu5965扫雷 枚举+递推

    题目链接 思路:枚举第一列的可能种数,然后递推即可,中途判断是否满足条件,最后再判断最后一列是否满足条件即可. #include<bits/stdc++.h> #define LL lon ...

  7. [数位DP]把枚举变成递推(未完)

    动态规划(DP)是个很玄学的东西 数位DP实际上 就是把数字上的枚举变成按位的递推 有伪代码 for i =这一位起始值 i<=这一位终止值 dp[这一位][i]+=dp[这一位-1][i]+- ...

  8. 【递推】【DFS】【枚举】Gym - 101246C - Explode 'Em All

    网格里放了一些石块,一个炸弹能炸开其所在的行和列.问炸光石块至少要几个炸弹. 枚举不炸开的行数,则可以得出还要炸开几列. 为了不让复杂度爆炸,需要两个优化. 先是递推预处理出f(i)表示i的二进制位中 ...

  9. 【NOIP模拟赛】【数学真奇妙】【递推】旅行者问题

    旅行者问题 [问题描述] lahub是一个旅行者的粉丝,他想成为一个真正的旅行者,所以他计划开始一段旅行.lahub想去参观n个目的地(都在一条直道上).lahub在起点开始他的旅行.第i个目的地和起 ...

随机推荐

  1. SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)

    一.Get $.ajax({ type: "get", url: "url地址", async: true, dataType:"json" ...

  2. 响应式瀑布流插件Grid-A-Licious

    Grid-A-Licious是一款遵守MIT协议的响应式瀑布流插件.该插件总代码行不超过400行,实现很巧妙,使用时也很流畅.实现原理也很简单,根据屏幕宽度和参数中设置的列宽度以及每项之间的间隔宽度, ...

  3. QAQ的LIS树 QAQ的LIS树2 题解报告

    这两道题实际上考试的时候是一道题OwO 太可怕了,忙了我三个多小时,写了整整7K 这个题两个询问关联性不强,所以分开来考虑 QAQ的LIS树 考虑如何用dp求解答案 设dp(v)表示v到根的修改后的序 ...

  4. 0-5v转0-20ma和0-5v转4-20ma

    0-5v转0-20ma 0-5v转4-20ma

  5. Python3 爬取微信好友基本信息,并进行数据清洗

    Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...

  6. 计算两个集合的差集——第六期 Power8 算法挑战赛

    第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...

  7. 01: git & github

    目录:GIT其他篇 01: git & github 02: git分支管理 目录: 1.1 常见版本管理工具介绍 及 版本工具作用 1.2 git.GitHub和SVN比较 1.3 本地gi ...

  8. html模板生成静态页面及模板分页处理

    它只让你修改页面的某一部分,当然这"某一部分"是由你来确定的.美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种 ...

  9. phpstorm怎么设置每个function都用那条横线隔开

  10. Java位运算实现加减乘除

    一.加法 a+b 举例实现:13+9=22 13+9不考虑进位结果为12 只考虑进位结果为10 和刚好是22. 13二进制为1101,9二进制为1001. 不考虑进位结果为0100.算式为a^b 只考 ...