【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表。。后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T.
看如家的分析没怎么看懂,后来发现这篇自己理解起来更容易(...)共享一下~http://blog.csdn.net/chenguolinblog/article/details/8873444
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。
编号0~(n-1)是有意义的,因为要模n,所以用0-(n-1)更好操作
我们知道第一个人(编号一定是(m-1) mod n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 ... n-2,n-1,0,1,2,... k-2
并且从k开始报0。
现在我们把他们的编号做一下转换:
k --> 0
k+1 --> 1
k+2 --> 2
...
...
k-2 --> n-2
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x'=(x+k) mod n;
所以我们只要一直重复这个过程便能求得最开始那个人的编号,因为这个人最终的编号是0(只剩他一个人):0->(0+k)%2->((0+k)%2+k)%3->......
f[n]=(f[n-1]+k)%n,f[1]=0; f[i]表示有i个人时,最后胜利者编号
==========================================
注意此上思路就适用于经典约瑟夫环问题。
==========================================
下面就要变形了...
现在这个问题是从m开始,即是首先(m-1)编号的人出去。。然后就和普通约瑟夫环一样了。故只要我们f[n]=(f[n-1]+m)%n单独算就行了。其他f[i]=(f[i-1]+k)%i;(1<i<n);
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn = ;
int f[maxn];
int main()
{
int n, k, m, step, cnt;
while(~scanf("%d%d%d", &n, &k, &m))
{
if(!n && !k && !m) break;
f[] = ;
for(int i = ; i < n; i++)
{
f[i] = (f[i-]+k)%i;
}
int ans = (f[n-]+m)%n;
printf("%d\n", ans+);
}
return ;
}
【约瑟夫环变形】UVa 1394 - And Then There Was One的更多相关文章
- HDU 5643 King's Game | 约瑟夫环变形
经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...
- Poj 3517 And Then There Was One(约瑟夫环变形)
简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- G - And Then There Was One (约瑟夫环变形)
Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...
- UVA 305 Joseph (约瑟夫环 打表)
Joseph The Joseph's problem is notoriously known. For those who are not familiar with the original ...
- HDU 5643 King's Game 【约瑟夫环】
题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...
- And Then There Was One(约瑟夫问题变形)
题目链接:http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS Memory Limit: 65536K ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
随机推荐
- 【转】iOS开发工具系列(按功能分)
http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...
- Delphi异形窗口之PNG
//1.单元内容 unit UnitAlienForm; interface uses Windows, Forms, Classes, Graphics; //从文件加载PNG procedure ...
- UVALive 7461 Separating Pebbles (计算几何)
Separating Pebbles 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/H Description http://7 ...
- thymeleaf中的Literals
Literals即为文字 一.Text literals:文本文字 文本文字只是字符串指定的单引号之间.他们可以包含任何字符,但你应避免任何单引号里面\ ' <p> Now you are ...
- lib 和 dll 的区别、生成以及使用详解
首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代码共享的方式. 静态库:在链接步骤中,连接器将从库文件取得所需的代码,复制到生成的可执行文件中,这种库称为静态库,其特点是可 ...
- python函数介绍
1.向函数传递信息 def greet_user(username): print('Hello,' + username.title() + '!') greet_user('jesse') 2.位 ...
- decide your linux OS is GUI or not
Try: ps -ef|grep X The ps command will display information about a selection of the active process ...
- uri中为什么本地文件file后面跟三个斜杠, http等协议跟两个斜杠?
那就要从URI[1]的结构来看了 scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] 可以看到,如果有host的时候,前面 ...
- [MODX] 2. Chunks $
Chunk in Modx can cut your template into samll pieces to make code reuseable. [[$chunk_name]] For ex ...
- Error creating bean with name 'menuController': Injection of autowired dependency……
出现了一大串错误 Error creating bean with name 'userController': Injection of autowired dependencies failed. ...