hdu 5643 BestCoder Round #75
King's Game
- 为了铭记历史,国王准备在阅兵的间隙玩约瑟夫游戏。它召来了 n(1\le n\le 5000)n(1≤n≤5000) 个士兵,逆时针围成一个圈,依次标号 1, 2, 3 ... n1,2,3...n。
- 第一轮第一个人从 11 开始报数,报到 11 就停止且报到 11 的这个人出局。
- 第二轮从上一轮出局的人的下一个人开始从 11 报数,报到 22 就停止且报到 22 的这个人出局。
- 第三轮从上一轮出局的人的下一个人开始从 11 报数,报到 33 就停止且报到 33 的这个人出局。
- 第 n - 1n−1 轮从上一轮出局的人的下一个人开始从 11 报数,报到 n - 1n−1 就停止且报到 n - 1n−1 的这个人出局。
- 最后剩余的人是幸存者,请问这个人的标号是多少?
- 第一行一个整数表示测试组数:T(0 < T\le5000)T(0<T≤5000) 。
- 每组数据占一行,包含一个整数 nn,表示 nn 个人围成一圈。
- 共 TT 行。对每组数据,输出幸存者的编号。
- 2
- 2
- 3
- 2
- 2
- 对于第一组数据,一开始报到 11 的人即标号为 11 的人退出,幸存者是 22 号。
- 对于第二组数据,一开始报到 11 的人即标号 11 的人退出。接着 22 号报 11,33 号报 22,报到 22 的人即 33 号退出。幸存者是 22 号。
- /*
- hdu 5643 BestCoder Round #75
- 这个类似于约瑟夫环,只是第i次报i的人被干掉
- 简单粗暴点就是打表,另外一种就是递推
- 我们要求的是n个人时最后存活的是谁f[n]
- 但是我们发现杀掉一个人之后从它的下一位开始重新编号
- 就成了求f[n-1]的解,所以考虑递推
- i+1 i i-1
- 0 1 2 3 4 5 6 7 -> 1 2 3 4 5 6 7 -> 1 3 4 5 6 7
- 0 1 2 3 4 5 6 5 0 1 2 3 4
- 对于第i次而言,我们要干掉第n-i+1个人,而且会从下一位重新编号
- 所以i->i+1时 每个数的编号相对增加了n-i+1 然后进行取模即可
- */
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <map>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- #define lson (i<<1)
- #define rson ((i<<1)|1)
- const int maxn = 5005;
- int dp[maxn];
- int main()
- {
- int T,m,n;
- scanf("%d",&T);
- while(T--)
- {
- dp[1] = 0;
- scanf("%d",&n);
- for(int i = 2; i <= n; i++)
- {
- dp[i] = (dp[i-1]+n-i+1)%i;
- }
- dp[n] += 1;
- printf("%d\n",dp[n]);
- }
- return 0;
- }
hdu 5643 BestCoder Round #75的更多相关文章
- hdu 5641 BestCoder Round #75
King's Phone Accepts: 310 Submissions: 2980 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- BestCoder Round #75 1001 - King's Cake
Problem Description It is the king's birthday before the military parade . The ministers prepared a ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 5640 King's Cake(BestCoder Round #75)
King's Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树
zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected ...
- HDU 5506 - BestCoder Round #60 - GT and set
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1003 题意 : 给N集 ...
- HDU 5505 - BestCoder Round #60 - GT and numbers
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...
- HDU 5496 - BestCoder Round #58 - Beauty of Sequence
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=637&pid=1002 思路 : 考 ...
- ACM学习历程—BestCoder Round #75
1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...
随机推荐
- Linux下ip配置与网络重启
ip配置 //以下ip配置重启失效 sudo ifconfig 192.168.1.1 sudo ifconfig 192.168.1.1 netmask 255.255.255.0 网络重启 //关 ...
- vue内置指令详解——小白速会
指令 (Directives) 是带有 v- 前缀的特殊属性,职责是,当表达式的值改变时,将其产生的连带影响,响应式地作用于 DOM. 内置指令 1.v-bind:响应并更新DOM特性:例如:v-bi ...
- 前端之bootstrap模态框
简介:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. Modal简介 Modal实现弹出表单 M ...
- 优化从 App.config 读取配置文件
public class AppSettingsConfig { /// <summary> ////// </summary> public static int Query ...
- java 细说String
String类内部用一个字符数组表示字符串,实例变量定义为: private final char value[]; String有两个构造方法,可以根据char数组创建String public S ...
- SpringCloud的服务注册中心(二)注册中心服务端和两个微服务应用客户端
一.构建EurekaServer工程 1.pom.xml 2.application.yml 3. EurekaServerApp.java 4.启动EurekaServer 二.构建部署 Eurek ...
- Angular UI框架 Ng-alain @delon的脚手架的生成开发模板
前言 首先感谢下 cipchk基于 Ng-Zorror 框架上制作的ng-alain . 之前很早就关注了 ng-alain,今天得空折腾了下. 折腾的时候发现官方文档有些坑,没有写清楚,所以我作为一 ...
- 集合之深入理解HashMap
Hashmap是一种非常常用的.应用广泛的数据类型 1.hashmap的数据结构 要知道hashmap是什么,首先要搞清楚它的数据结构,在java编程语言中,最基本的结构就是两种,一个是数组,另外一个 ...
- 关于 Integer 值比较的问题
今天刚好遇到这样的问题,别的不说,先上代码 public class TestInteger { public static void main(final String[] args) { fina ...
- Java-Maven(七):Eclipse中Maven依赖、聚合、继承特性
之前通过学习了解,maven集成到eclipse中的如何创建项目,以及maven命令插件在eclipse中安装后的用法.那么接下来我们将会学习一些maven在项目中的一些特性,及如何使用. Maven ...