约瑟夫问题专题

 

UVa 1452 - Jump(约瑟夫环变形)

分类: 动态规划   538人阅读  评论(0)  收藏  举报

目录(?)[-]

  1. 题意
  2. 思路
  3. 代码
    1. 来自CODE的代码片 uvaa-1452-Jumpcpp


本文出自   http://blog.csdn.net/shuangde800



题目点击打开链接


题意

把1~n按逆时针顺序排成一个圆圈,从1开始没k个数字删除掉一个,知道所有数字都删完。

求最后删除的3个数。


思路

我们已经知道了,怎么可以推出最后一个被删除的编号(可参考百度百科)

http://baike.baidu.com/view/213217.htm

f(1) = 0, 表示最后还剩下一个时,这个编号为0

f(n) = (f(n-1) + m) % n

那么保存最后第1,2,3个数,一直推到第一个即可。


代码

<a target=_blank id="L1" href="http://blog.csdn.net/shuangde800/article/details/9745849#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/shuangde800/article/details/9745849#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/shuangde800/article/details/9745849#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/shuangde800/article/details/9745849#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/shuangde800/article/details/9745849#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/shuangde800/article/details/9745849#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/shuangde800/article/details/9745849#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/shuangde800/article/details/9745849#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/shuangde800/article/details/9745849#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/shuangde800/article/details/9745849#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/shuangde800/article/details/9745849#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/shuangde800/article/details/9745849#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/shuangde800/article/details/9745849#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/shuangde800/article/details/9745849#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/shuangde800/article/details/9745849#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/shuangde800/article/details/9745849#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/shuangde800/article/details/9745849#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/shuangde800/article/details/9745849#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/shuangde800/article/details/9745849#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/shuangde800/article/details/9745849#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/shuangde800/article/details/9745849#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/shuangde800/article/details/9745849#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/shuangde800/article/details/9745849#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/shuangde800/article/details/9745849#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/shuangde800/article/details/9745849#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/shuangde800/article/details/9745849#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/shuangde800/article/details/9745849#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/shuangde800/article/details/9745849#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/shuangde800/article/details/9745849#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/shuangde800/article/details/9745849#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/shuangde800/article/details/9745849#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/shuangde800/article/details/9745849#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/shuangde800/article/details/9745849#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/shuangde800/article/details/9745849#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/shuangde800/article/details/9745849#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/shuangde800/article/details/9745849#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/shuangde800/article/details/9745849#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/shuangde800/article/details/9745849#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/shuangde800/article/details/9745849#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank id="L40" href="http://blog.csdn.net/shuangde800/article/details/9745849#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank id="L41" href="http://blog.csdn.net/shuangde800/article/details/9745849#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
<a target=_blank id="L42" href="http://blog.csdn.net/shuangde800/article/details/9745849#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a>
<a target=_blank id="L43" href="http://blog.csdn.net/shuangde800/article/details/9745849#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a>
<a target=_blank id="L44" href="http://blog.csdn.net/shuangde800/article/details/9745849#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a>
<a target=_blank id="L45" href="http://blog.csdn.net/shuangde800/article/details/9745849#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a>
<a target=_blank id="L46" href="http://blog.csdn.net/shuangde800/article/details/9745849#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a>
<a target=_blank id="L47" href="http://blog.csdn.net/shuangde800/article/details/9745849#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a>
<a target=_blank id="L48" href="http://blog.csdn.net/shuangde800/article/details/9745849#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a>
<a target=_blank id="L49" href="http://blog.csdn.net/shuangde800/article/details/9745849#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a>
<a target=_blank id="L50" href="http://blog.csdn.net/shuangde800/article/details/9745849#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a>
<a target=_blank id="L51" href="http://blog.csdn.net/shuangde800/article/details/9745849#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a>
<a target=_blank id="L52" href="http://blog.csdn.net/shuangde800/article/details/9745849#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a>
<a target=_blank id="L53" href="http://blog.csdn.net/shuangde800/article/details/9745849#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a>
<a target=_blank id="L54" href="http://blog.csdn.net/shuangde800/article/details/9745849#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a>
<a target=_blank id="L55" href="http://blog.csdn.net/shuangde800/article/details/9745849#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a>
<a target=_blank id="L56" href="http://blog.csdn.net/shuangde800/article/details/9745849#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a>
<a target=_blank id="L57" href="http://blog.csdn.net/shuangde800/article/details/9745849#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;"> 57</a>
<a target=_blank id="L58" href="http://blog.csdn.net/shuangde800/article/details/9745849#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;"> 58</a>
<a target=_blank id="L59" href="http://blog.csdn.net/shuangde800/article/details/9745849#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;"> 59</a>
<a target=_blank id="L60" href="http://blog.csdn.net/shuangde800/article/details/9745849#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;"> 60</a>
<a target=_blank id="L61" href="http://blog.csdn.net/shuangde800/article/details/9745849#L61" rel="#L61" style="color: rgb(102, 102, 102); text-decoration: none;"> 61</a>
<a target=_blank id="L62" href="http://blog.csdn.net/shuangde800/article/details/9745849#L62" rel="#L62" style="color: rgb(102, 102, 102); text-decoration: none;"> 62</a>
             
             
             
             
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source: uva-1452 Jump
* @author: shuangde
* @blog: blog.csdn.net/shuangde800
* @email: zengshuangde@gmail.com
*===========================================*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std ;
typedef long long int64 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos ( - 1.0 );
const int MAXN = 110 ;
int n , m ;
int main (){
int T ;
scanf ( "%d" , & T );
while ( T -- ){
scanf ( "%d%d" , & n , & m );
int ans1 = 0 , ans2 , ans3 ;
for ( int i = 2 ; i <= n ; ++ i ){
ans1 = ( ans1 + m ) % i ;
if ( i == 2 ){
// 当剩下最后2个数时,编号为0,1, 推出倒数第二个删除的数当前值
ans2 = ! ans1 ;
} else if ( i == 3 ){
// 当剩下最后3个数时,编号为0,1,2, 推出倒数第三个删除的数当前值
ans2 = ( ans2 + m ) % i ;
bool vis [ 3 ];
memset ( vis , 0 , sizeof ( vis ));
vis [ ans1 ] = vis [ ans2 ] = true ;
for ( int j = 0 ; j < 3 ; ++ j ) if ( ! vis [ j ]){
ans3 = j ; break ;
}
} else {
ans2 = ( ans2 + m ) % i ;
ans3 = ( ans3 + m ) % i ;
}
}
ans1 = ( ans1 + 1 ) % n ;
ans2 = ( ans2 + 1 ) % n ;
ans3 = ( ans3 + 1 ) % n ;
printf ( "%d %d %d \n " , ans3 ? ans3 : n , ans2 ? ans2 : n , ans1 ? ans1 : n );
}
return 0 ;
}
 来自CODE的代码片
uvaa-1452-Jump.cpp

你可能感兴趣的:(动态规划,C语言,ACM,uva,约瑟夫问题)