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?
Theinputwillconsistofasequenceofintegers,eachrepresentingthe
numberofcoconutsgatheredbyagroupofpersons(andamonkey)that
wereshipwrecked.Thesequencewillbefollowedbyanegativenumber.
Foreachnumberofcoconuts,determinethelargestnumberofpersons
whocouldhaveparticipatedintheproceduredescribedabove.Display
theresultssimilartothemannershownbelow,intheSampleOutput.
Theremaybenosolutionforsomeoftheinputcases;ifso,statethat
observation.
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,eachrepresentingthenumberofcoconutsgatheredbyagroupofpersons(andamonkey)that
wereshipwrecked.Thesequencewillbefollowedbyanegativenumber.
Output
Foreachnumberofcoconuts,determinethelargestnumberofpersonswhocouldhaveparticipatedintheproceduredescribedabove.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; }
相关文章推荐
- 使用RecyclerView实现列表展开动画
- JVM学习资料收集
- Android Studio 中获取SHA1值
- SecureCRT 连接本地linux虚拟机
- iOS 与 Android 生命周期对比
- HDU 2041 超级楼梯
- java并发编程实战第五章(3)异步运行任务
- linux把光盘复制成ISO文件方法
- PigSPS: a database for pig SNPs and signatures of positive selection
- jQuery插件开发方式
- maven入门
- note
- MACOS无限试用Cornerstone的方法
- Search for a Range
- sql 分页存储过程
- dialog弹出,点击back按键无法返回问题解决
- HDU 5308 I Wanna Become A 24-Point Master(2015多校第二场)
- 五种基于RGB色彩空间统计的皮肤检测算法
- 五种基于RGB色彩空间统计的皮肤检测算法
- 实习16天