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. MySQL Crash Course #12# Chapter 18. Full-Text Searching

    INDEX 由于性能.智能结果等多方面原因,在搜索文本时,全文搜索一般要优于通配符和正则表达式,前者为指定列建立索引,以便快速找到对应行,并且将结果集智能排序.启用查询扩展可以让我们得到未必包含关键字 ...

  2. ELK学习笔记之ELK分析nginx日志

    0x00 配置FIlebeat搜集syslog #安装 rpm -ivh filebeat-6.2.3-x86_64.rpm mv /etc/filebeat/filebeat.yml /etc/fi ...

  3. 编译安装vsftpd-3.0.2

    编译安装vsftpd 首先下载源码包(我一般喜欢放在/home/test) 解压:tar -zxvf vsftpd-3.0.2.tar.gz 进入目录进行编译 cd vsftpd-3.0.2 编译之前 ...

  4. 自动对比度的opencv实现

    在http://www.cnblogs.com/Imageshop/archive/2011/11/13/2247614.html 一文中,作者给出了“自动对比度”的实现方法,非常nice 实际实现过 ...

  5. poj 2369 Permutations - 数论

    We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...

  6. C++ 单例模式(转载)

    转载:http://www.cnblogs.com/cxjchen/p/3148582.html 转载:http://blog.csdn.net/hackbuteer1/article/details ...

  7. jQuery 中 $( ) 函数的用法总结

    摘要 jQuery对象: 具有jquery框架设置的所有功能的调用者, 就是该框架的对象 $又是什么?: $就是jQuery对象, jQuery对象为window的全局属性, 所以可以直接使用 如何自 ...

  8. c#之有参和无参构造函数,扩展方法

    例如在程序中创建 Parent类和Test类,在Test有三个构造函数,parent类继承Test类,那么我们可以在Test类自身中添加 扩展 方法吗? 答案:是不可以的.因为扩展方法必须是静态的,且 ...

  9. BZOJ1966: [Ahoi2005]VIRUS 病毒检测 Trie+搜索

    Description 科学家们在Samuel星球上的探险仍在继续.非常幸运的,在Samuel星球的南极附近,探险机器人发现了一个巨大的冰湖!机器人在这个冰湖中搜集到了许多RNA片段运回了实验基地.科 ...

  10. java中Scanner类nextInt之后用nextLine无法读取输入

    http://blog.csdn.net/wjy1090233191/article/details/42080029 这篇文章写得非常详细和准确