首先约瑟夫问题的数学推理过程:我们知道第一个人(编号一定是(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)个人,报到为(M-1)的人自杀,问题规模减小了。这样一直进行下去,最后剩下编号为0的人。用函数表示:

F(1)=0

当有2个人的时候(N=2),报道(M-1)的人自杀,最后自杀的人是谁?应该是在只有一个人时,报数时得到的最后自杀的序号加上M,因为报到M-1的人已经自杀,只剩下2个人,另一个自杀者就是最后自杀者,用函数表示:

F(2)=F(1)+M

可以得到递推公式:

F(i)=F(i-1)+M

因为可能会超出总人数范围,所以要求模

F(i)=(F(i-1)+M)%i

知道了约瑟夫问题的原理之后;

题意:给你n个人,每个人报1,2,,,,,n,报道数的人被处死,求最后活着的人的编号
根据约瑟夫问题,可以知道每次都是倒推回去;
所以将m改变成n-1,n-2,n-3,....1;

# include <stdio.h>
int main()
{
int m, n, i, s;
int x;
scanf("%d",&x);
while(x--)
{
scanf("%d", &n);
s=0;
m=n-1;
for (i = 2; i <= n; ++i)
{
s=(s + m)%i;
m--;
}
printf("%d\n", s+1);
}
return 0;
}

  

hdu 5643 King's Game 约瑟夫变形的更多相关文章

  1. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  2. HDU 5643 King's Game 【约瑟夫环】

    题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...

  3. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  4. hdu 5643 BestCoder Round #75

    King's Game  Accepts: 249  Submissions: 671  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  5. HDU 3094 树上删边 NIM变形

    基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...

  6. HDU 5642 King's Order dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order  Accepts: 381  Submissions: 1361   ...

  7. HDU 5644 King's Pilots 费用流

    King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...

  8. HDU 5642 King's Order 动态规划

    King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...

  9. HDU 5640 King's Cake GCD

    King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...

随机推荐

  1. React笔记_(2)_react语法1

    这一节内容主要以了解为主. 渐渐的体会react的语法和其特性. ① htmlAndJs 混合编写 react和以往的前后台书写方式不一样. 在之前的多个语言中,讲求的是将页面代码和js代码逻辑分开, ...

  2. centos7重启rsyslog服务|centos7重启syslog服务

    centos7重启rsyslog服务: systemctl restart rsyslog 使用:(killall无效) killall -HUP rsyslog

  3. PHP的Socket通信之UDP篇

    1.创建一简单的UDP服务器 //服务器信息 $server = 'udp://127.0.0.1:9998'; //消息结束符号 $msg_eof = "\n"; $socket ...

  4. ecshop订单打印页显示商品缩略图和序号

    ecshop订单打印页显示商品缩略图和序号 订单打印页显示商品缩略图,在论坛没找到适合2.7.2相关的文章,特意贴上来给大家研究一下.1.找到 $sql = "SELECT o.*, IF( ...

  5. Linux下Date命令的用法

    转自http://blog.chinaunix.net/uid-8223172-id-2511672.html linux下date的用法比较复杂,但是也用的比较多,尤其是shell里面.现总结一下自 ...

  6. tar 报错gzip: stdin: not in gzip format

    今天在linux下 用tar -zxvf xxx.tar.bz2 然后就报这个错. gzip: stdin: not in gzip formattar: Child returned status ...

  7. URL List

    wifi driver http://wenku.baidu.com/view/5fb275e9b8f67c1cfad6b85e.html http://wenku.baidu.com/view/a5 ...

  8. EF 用CallContext上下文管理

    public class ObjectContextFactory { private static CIK_NewsEntities context; public static DbContext ...

  9. SDUT 2408:Pick apples

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...

  10. easyui datebox 只选择年月

    //将日期输入框变为年月的函数方法    var month=0;      $('#effectiveDate').datebox({        onShowPanel: function () ...