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. 什么是公网IP、内网IP和NAT转换?

    搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地址)和内网IP(即局域网IP地址),但他们的区别是什么? 1.引言 搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地 ...

  2. C++中公有继承、保护继承、私有继承的区别

    公有继承时基类中各成员属性保持不变,基类中private成员被隐藏.派生类的成员只能访问基类中的public/protected成员,而不能访问private成员:派生类的对象只能访问基类中的publ ...

  3. 手撕vue-cli配置——webpack.prod.conf.js篇

    'use strict' const path = require('path') const utils = require('./utils') const webpack = require(' ...

  4. Redis 如何保持和MySQL数据一致【二】

    需求起因 在高并发的业务场景下,数据库大多数情况都是用户并发访问最薄弱的环节.所以,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问MySQL等数据库. 这个业务场景,主要 ...

  5. swagger报错No handler found for GET /swagger-ui.html

    今天下载jeeweb框架下来研究,其他还有,就是swagger老是出不来.报错:No handler found for GET /swagger-ui.html 后来搜索才发现,这个错误,是因为资源 ...

  6. PHP_SELF变量解析和重复路径解决

    最近升级PHP到PHP7版本,并重新部署了新的Nginx,启动的时候发现了一个问题,全局变量$_SERVER['PHP_SELF']的值发生了改变,从而影响到代码的功能.因此我们来了解下$_SERVE ...

  7. ubuntu下桌面假死处理方法(非重启)

    一.背景 2018/05/22,就在这一天,进入ubuntu的桌面后随便点击任何位置均无法响应,此时又不想重启,遂出此文 二.解决方案 2.1 关掉Xorg进程 2.1.1按下ctrl+alt+F1进 ...

  8. Git 同时与多个远程库互相同步

    情形:有两个git服务器,比如github,gitosc,有个项目同时在两个服务器上,要互相同步 其实命令还是比较简单的,比如一个现有的git项目,在github,gitosc中分别创建好对应的项目. ...

  9. UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解

    思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...

  10. hdu 3874(树状数组)题解

    Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ba ...