您的位置:首页 > 其它

Coconuts, Revisited(递推+枚举+模拟)

2015-07-24 10:20 197 查看
Description

TheshortstorytitledCoconuts,byBenAmesWilliams,appearedintheSaturdayEveningPostonOctober9,1926.Thestorytellsaboutfivemenandamonkeywhowereshipwreckedonanisland.Theyspentthefirstnightgatheringcoconuts.Duringthenight,onemanwokeupanddecidedtotakehisshareofthecoconuts.Hedividedthemintofivepiles.Onecoconutwasleftoversohegaveittothemonkey,thenhidhisshareandwentbacktosleep.
Soonasecondmanwokeupanddidthesame
thing.Afterdividingthecoconutsintofivepiles,onecoconutwasleft
overwhichhegavetothemonkey.Hethenhidhisshareandwentback
tobed.Thethird,fourth,andfifthmanfollowedexactlythesame
procedure.Thenextmorning,aftertheyallwokeup,theydividedthe
remainingcoconutsintofiveequalshares.Thistimenococonutswere
leftover.
Anobviousquestionis``howmanycoconuts
didtheyoriginallygather?"Thereareaninfinitenumberofanswers,
butthelowestoftheseis3,121.Butthat'snotourproblemhere.

Supposeweturntheproblemaround.Ifweknowthenumberofcoconuts
thatweregathered,whatisthemaximumnumberofpersons(andone
monkey)thatcouldhavebeenshipwreckedifthesameprocedurecould
occur?

Input

Theinputwillconsistofasequenceofintegers,eachrepresentingthe
numberofcoconutsgatheredbyagroupofpersons(andamonkey)that
wereshipwrecked.Thesequencewillbefollowedbyanegativenumber.

Output

Foreachnumberofcoconuts,determinethelargestnumberofpersons
whocouldhaveparticipatedintheproceduredescribedabove.Display
theresultssimilartothemannershownbelow,intheSampleOutput.
Theremaybenosolutionforsomeoftheinputcases;ifso,statethat
observation.

SampleInput

25
30
3121
-1

SampleOutput

25coconuts,3peopleand1monkey
30coconuts,nosolution
3121coconuts,5peopleand1monkey

题目大意:还是人和猴子分桃子,不过猴子只有一个。和UVA-10726CocoMonkey不同的是,这道题已知的是桃子数,让求可能的最多人数。
题目解析:将递推的过程反过来,枚举人数,模拟分桃子的过程。人数不会太多。

代码如下:


#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<fstream>
#include<cstring>
#include<algorithm>
usingnamespacestd;
constintN=100000005;
boolok(intss,intn)
{
intt=ss;
while(ss--){
if((n-1)%t)
break;
n=(n-1)/t*(t-1);
}
if(ss==-1&&(n%t==0))
returntrue;
returnfalse;
}
intmain()
{
intn,i;
while(scanf("%d",&n))
{
if(n==-1)
break;
intans=0;
for(i=2;i<10;++i){
if(i*(i-1)>=n)
break;
if(ok(i,n)){
ans=i;
}
}
printf("%dcoconuts,",n);
if(ans>0){
printf("%dpeopleand1monkey\n",ans);
}else
printf("nosolution\n");
}
return0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: