例26 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
来源:互联网 发布:屠龙大陆翅膀进阶数据 编辑:程序博客网 时间:2024/06/05 02:42
题目描述
n个数排成一个圈。第一次删除m,以后每k个数删除一次,求最后一个被删除的数。
题目分析
首先为了取模,n个人编号为0,1,2......n-1,当只有1个人时,肯定是编号为0的(只有它),所以dp[1]=0,
当有两个人时,最后剩下的(dp[1]+k)%2,就是反向思考的过程,于是dp[i]=(dp[i-1]+k)%i;
还有就是由于第一次是删除的m,那么还要将这个差值补上,于是ans=(dp[n]+k-m)%n;由于k,m差
值可能是很小的负数,这样ans就为负数了,所以当ans<0时,ans+=n,最后将ans+1,将为了取模方便
减的1补上。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100000+100;int dp[maxn];int main(){ int n,m,k; while(~scanf("%d%d%d",&n,&k,&m)&&n) { dp[1]=0; for(int i=2;i<=n;i++) dp[i]=(dp[i-1]+k)%i; int ans=(dp[n]+(m-k))%n; if(ans<0) ans+=n; printf("%d\n",ans+1); } return 0;}
0 0
- 例26 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- LA 3882 - And Then There Was One 【约瑟夫环变形】
- LA 3882 And Then There Was One 约瑟夫变形 *
- 约瑟夫问题变形 And Then There was One, LA 3882 递推 动态规划
- And Then There Was One ,Japan LA 3882
- UVALive - 3882 And Then There Was One 约瑟夫的变形问题
- LA 3882 - And Then There Was One(约瑟夫 递归)
- And Then There Was One +dp+约瑟夫环的变形
- UVALive 3882--And Then There Was One+约瑟夫环问题变形
- LA-3882 And Then There Was One
- LA 3882 And Then There Was One
- UVALive 3882 And Then There Was One 约瑟夫环问题
- POJ 3157 And Then There Was One【约瑟夫变形】
- UVA 1394 And Then There Was One(约瑟夫环变形)
- POJ 3517 And Then There Was One (递推,约瑟夫问题变形)
- 1394 - And Then There Was One(约瑟夫问题变形)DP
- poj 3517 And Then There Was One 约瑟夫问题
- HDU1075 What Are You Talking About 【Map容器】
- java第三天,代码好多哦~~
- java数据引擎(八):应用一
- JAVA&Android随记
- UVA 12034-Race
- 例26 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- Android中自定义对话框Dialog
- ArrayList和Vector的深度解读
- android开发步步为营之54:读取assets,raw文件夹下文件
- php安全
- 【HDU 1042】N! —— 高精度整数
- svn的建立以及使用
- thinkphp3.1.3多对多关联模型BUG修复
- UI UIActionSheet