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. python之路----模块调用

    如何使用模块? 1 import 示例文件:自定义模块my_module.py,文件名my_module.py,模块名my_module #my_module.py print('from the m ...

  2. C++算法原理与实践(面试中的算法和准备过程)

    第0部分 简介 1. 举个例子:面试的时候,可能会出一道算法考试题,比如写一个 strstr 函数——字符串匹配. 可能会想到用KMP算法来解题,但是该算法很复杂,不适宜在面试中使用. 1.1 C++ ...

  3. CP2102

    1概述 CP2102其集成度高,内置USB2.0全速功能控制器.USB收发器.晶体振荡器.EEPROM及异步串行数据总线(UART),支持调制解调器全功能信号,无需任何外部的USB器件.CP2102与 ...

  4. Duilib应用修改程序图标方法(转载)

    转载:http://www.cnblogs.com/lanzhi/p/6468596.html 本文向大家介绍如何修改duilib应用图标,对于win32或者mfc应用来说,我们可以在注册窗口类时指定 ...

  5. SQLSERVER中order by ,group by ,having where 的先后顺序

    SELECT [Name]  FROM [LinqToSql].[dbo].[Student]  where name='***' group  by  name    having (name='* ...

  6. ACM-ICPC 2018 徐州赛区网络预赛A Hard to prepare(DP)题解

    题目链接 题意:有n个格子拉成一个环,给你k,你能使用任意个数的0 ~ 2^k - 1,规定操作 i XNOR j 为~(i  ^  j),要求相邻的格子的元素的XNOR为正数,问你有几种排法,答案取 ...

  7. POJ 1236 Network of Schools(tarjan)题解

    题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...

  8. Springboot2.x 拦截器

    一,单个拦截器,实现接口  HandlerInterceptor @Component public class MyInterceptor1 implements HandlerIntercepto ...

  9. KMP 初级板子 待更新

    复杂度 O(n+m) 这个博主写的蛮不错的 http://www.cnblogs.com/SYCstudio/p/7194315.html 1.本文中,所有的字符串从0开始编号2.为了在程序中表示方便 ...

  10. Java 类引入 学习记录规整

    之前觉得声明一个类,再把另一个包内的声明数值用第一个类打印出来就可以了(加入引入包类) 结果发现是不对的 看了看demo  得出正确结果    ImportTest 被运行 引入下面的Import类 ...