UVALive 3882--And Then There Was One+约瑟夫环问题变形
来源:互联网 发布:ata办公软件高级 编辑:程序博客网 时间:2024/05/21 07:04
题目链接:点击进入
题目意思大概和约瑟夫环问题差不多,唯一的不同点在于起点改成了m;刚开始的时候我想直接链表模拟算了,但是后面一看,数据太大,就改用公式做了。约瑟夫环的公式是:f(n)=(f(n-1)+k)%n
,对于这个题起点为m,所以答案就会变成ans=(f(n)+m-k+1)%n;
ans有可能小于0,此时我们要给他加上一个n,ans+=n。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<algorithm>using namespace std;const int maxn=11000;int a[maxn];int main(){ //freopen("in.txt","r",stdin); int n,m,k; while(scanf("%d%d%d",&n,&k,&m)!=EOF) { if(!m&&!n&&!k) break; a[1]=0; for(int i=2;i<=n;i++) a[i]=(a[i-1]+k)%i; int ans=(a[n]+m-k+1)%n; if(ans<=0) ans+=n; printf("%d\n",ans); } return 0;}
1 0
- UVALive 3882--And Then There Was One+约瑟夫环问题变形
- UVALive 3882 And Then There Was One 约瑟夫环问题
- UVALive - 3882 And Then There Was One 约瑟夫的变形问题
- LA 3882 - And Then There Was One 【约瑟夫环变形】
- LA 3882 And Then There Was One 约瑟夫变形 *
- And Then There Was One +dp+约瑟夫环的变形
- UVA 1394 And Then There Was One(约瑟夫环变形)
- 约瑟夫问题变形 And Then There was One, LA 3882 递推 动态规划
- 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- 例26 约瑟夫问题的变形(And Then There Was One,Japan 2007,LA 3882)
- UVALive 3882 And Then There Was One
- UVALive 3882 And Then There Was One
- UVALive 3882 And Then There Was One
- And Then There Was One----约瑟夫环
- POJ 3157 And Then There Was One【约瑟夫变形】
- uva live 3882 And Then There Was One 约瑟夫环
- POJ 3517 And Then There Was One (递推,约瑟夫问题变形)
- 1394 - And Then There Was One(约瑟夫问题变形)DP
- 转载:Hadoop Streaming高级编程
- Unity3D如何集成ShareSDK
- Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException:
- IOS开发系列--Objective-C之KVC、KVO
- python入门sorted,lambda函数
- UVALive 3882--And Then There Was One+约瑟夫环问题变形
- 用Eclipse开发Java程序入门
- mysql 修改默认存储位置
- 西安公积金1
- 2012年5月SAT香港真题解析
- 【iOS学习】一、随机数的使用
- Android Fragment 基本介绍
- 高级数据结构-树状数组总结
- 招聘一个靠谱的 iOS(含面试题)