Coconuts, Revisited

          Coconuts, Revisited

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

题意:总共有n个椰子,(自己以为是可可豆)有m个人,到了晚上,每个人都会去把n-1(给猴子一个)除以m,再拿走自己的一份,最后剩的还要是m的倍数,求最大m如:25……先减一……=24……再除以m(=3)……=8……第一个人拿走了8个,剩下了16个……第二个人也要拿……-1再除3为5……即第二个人拿走了5个……还剩10个……-1再除3为3……即第三个人拿走了3个……还剩6个……没人拿了,而且剩下了3的倍数tip:模拟,下一次剩的椰子等于这一次减去这个人拿的再减一;即x=x-(x-1)/i-1
 1 #include<iostream>
 2
 3 using namespace std;
 4
 5 int solve(int n)
 6 {
 7     int i,j,x;
 8     for(i=10;i>1;i--)
 9     {
10         x=n;
11         j=i;
12         while(j)
13         {
14             if((x-1)%i!=0)
15                 break;
16             x=x-(x-1)/i-1;
17             j--;
18         }
19         if(j==0&&x%i==0)
20             return i;
21     }
22     return 0;
23 }
24
25 int main()
26 {
27     int c,p;
28     while(cin>>c)
29     {
30         if(c==-1)
31             break;
32         cout<<c<<" coconuts, ";
33         p=solve(c);
34         if(p)
35             cout<<p<<" people and 1 monkey"<<endl;
36         else
37             cout<<"no solution"<<endl;
38     }
39     return 0;
40 }

				
时间: 2024-08-04 23:47:36

Coconuts, Revisited的相关文章

uva 616 - Coconuts, Revisited(数学)

题目链接:uva 616 - Coconuts, Revisited 题目大意:题目背景和uva 10726是一样的,只是这道题目是给出n,表示椰子的个数,并且猴子的个数为1,问说是否能找到满足的人数,并且要求人数尽量大. 解题思路:枚举人数,然后根据uva 10726推出的公式去求出最后剩下的椰子是否满足平分等判断. #include <cstdio> #include <cstring> #include <cmath> #include <iostream&

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.

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

hdu 5929 Coconuts 离散化+dfs

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts

2016 长春东北赛---Coconuts(离散化+DFS)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5925 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessb

UVA 10537 - The Toll! Revisited(dijstra扩展)

UVA 10537 - The Toll! Revisited 题目链接 题意:给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交1,给定起点终点和到目标地点要剩下的货物,问最少要带多少货物上路,并输出路径,如果有多种方案,要求字典序最小 思路:dijstra的逆向运用,d数组含义变成到该结点至少需要这么多货物,然后反向建图,从终点向起点反向做一遍 这题被坑了..并不是输出的城市才存在,比如下面这组样例 0 1 A A 应该输出 1 A 代码: #i

uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母.首先输入m条边,当经过小写字母时需要付一单位的过路费,当经过大写字母时,要付当前财务的1/20做过路费.问在起点最少需要带多少物品使到达终点时还有k个物品.当有多条符合条件的路径时输出字典序最小的一个. 思路:已知终点的权值,那么可以从终点向前推.求终点到起点的最短路径,然后按字典序打印路径. 比较难处理的是:向前推时前驱节点的权值计算.列个方程算算就可以了,主要时不能整除的情况. 计算前驱结点dis值的时候,