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 ...
随机推荐
- HTML5文件操作API
HTML5文件操作API 一.文件操作API 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或 ...
- io多路复用(三)
#!/usr/bin/env python # -*- coding:utf-8 -*- import socket sk1 = socket.socket() sk1.bind(('127.0.0. ...
- NetFPGA-1G-CML从零开始环境配置
NetFPGA-1G-CML从零开始环境配置 前言 偶得一块NetFPGA-1G-CML,跟着github对NetFPGA-1G-CML的入门指南,一步步把配置环境终于搭建起来,下面重新复现一下此过程 ...
- Microsoft dynamic sdk中join应该注意的问题.
QueryExpression queryNextSeq = new QueryExpression { EntityName = "ep_prodoperationsequence&quo ...
- 《网络》:设置三个密码:通过console口连接设备,进入特权模式,登录Telnet
软件:Cisco Packet Tracer Instructor 软件下载链接在上一篇文章中. 内容:通过设置三个密码,熟悉采用Telnet方式配置交换机的方法. 细节说明:计算机的IP地址和交换机 ...
- JS 上传图片时实现预览
网页中一张图片可以这样显示: <img src="http://www.letuknowit.com/images/wg.png"/>也可以这样显示:<img s ...
- Linux入门:vi 和 vim
vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器. 本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了! 转自:http://www.l ...
- 记录下项目中常用到的JavaScript/JQuery代码一(大量实例)
一直没有系统学习Javascript和Jquery,每次都是用到的时候去搜索引擎查,感觉效率挺低的.这边把我项目中用的的记录下,想到哪写哪,有时间再仔细整理. 当然,由于我主要是写后端java开发,而 ...
- 详解Class
Classs是es6提供的类,相当于es5的构造函数. 写法: class Foo { constructor () { // new 的时候会调用该方法,可以通过return改变构造函数的返回值 r ...
- TSQL:A表字段与B表中的关联,关联条件中一列是随机关联的实现方式
A表字段与B表中的关联,关联条件中一列是随机关联的实现方式 create table test( rsrp string, rsrq string, tkey string, distan strin ...