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 ...
随机推荐
- router问题
var http = require("http"); var router = require("./router.js"); //创建服务器 var ser ...
- edittext实现自动查询,刷新listview
mEdittextqueryvalue.addTextChangedListener(new TextWatcher() { @Override pub ...
- tableView//collectionView加载时的动画
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( ...
- NetFPGA-1G-CML从零开始环境配置
NetFPGA-1G-CML从零开始环境配置 前言 偶得一块NetFPGA-1G-CML,跟着github对NetFPGA-1G-CML的入门指南,一步步把配置环境终于搭建起来,下面重新复现一下此过程 ...
- api-gateway实践(06)新服务网关 - 请求监控
一.实时监控 用户点击服务实例,系统显示服务实例-version下的api列表, 用户点击某个api的如下两个图标 1.API请求次数监控 横轴:时间,粒度为分钟 纵轴:请求访问次数 展示:失败数(红 ...
- 大数据学习总结(7)we should...
大数据场景一.各种标签查询 查询要素:人.事.物.单位 查询范围:A范围.B范围.... 查询结果:pic.name.data from 1.痛点:对所有文本皆有实时查询需求2.难点:传统SQL使用W ...
- loadrunner录制时web时,安全证书问题
测试环境:win7+LoadRunner11+ie9 遇到的问题:用LoadRunner录制时,打开百度,总是报安全证书问题,如图所示 解决方法:Tools——Recording Options——p ...
- Struts(十):OGNL表达式(一)
Struts2 用s:porperty标签和OGNL表达式来读取值栈中的属性值: I.值栈中的属性值: 1.对象栈:读取对象栈中的某一个对象的属性值: 2.Map栈 :request,session, ...
- WPF Uri
场景:自定义控件Generic.xaml样式引用资源字典Dictionary1.xaml. 方式:绝对路径. 方式1: <ResourceDictionary> <ResourceD ...
- Java内存回收机制.md
1.java的内存 java的内存结构分为 堆 (是gc的主要区域) 线程共享,主要是用于分配实例对象和数组 栈 线程私有,它的生命周期和线程相同,又分成 虚拟机栈和本地方法栈,只有它会报 Stack ...