hdu5463---King's Game
来源:互联网 发布:mt4 一键下单脚本源码 编辑:程序博客网 时间:2024/05/23 00:08
King's Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 808 Accepted Submission(s): 444
Problem Description
In order to remember history, King plans to play losephus problem in the parade gap.He callsn(1≤n≤5000) soldiers, counterclockwise in a circle, in label 1,2,3...n .
The first round, the first person with label1 counts off, and the man who report number 1 is out.
The second round, the next person of the person who is out in the last round counts off, and the man who report number2 is out.
The third round, the next person of the person who is out in the last round counts off, and the person who report number3 is out.
The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report numbern−1 is out.
And the last man is survivor. Do you know the label of the survivor?
The first round, the first person with label
The second round, the next person of the person who is out in the last round counts off, and the man who report number
The third round, the next person of the person who is out in the last round counts off, and the person who report number
The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report number
And the last man is survivor. Do you know the label of the survivor?
Input
The first line contains a number T(0<T≤5000) , the number of the testcases.
For each test case, there are only one line, containing one integern , representing the number of players.
For each test case, there are only one line, containing one integer
Output
Output exactly T lines. For each test case, print the label of the survivor.
Sample Input
223
Sample Output
22Hint:For test case #1:the man who report number $1$ is the man with label $1$, so the man with label $2$ is survivor.For test case #1:the man who report number $1$ is the man with label $1$, so the man with label 1 is out. Again the the man with label 2 counts $1$, the man with label $3$ counts $2$, so the man who report number $2$ is the man with label $3$. At last the man with label $2$ is survivor.
思路: 模板题
可百度约瑟夫环的原理
由题意可得递推公式:
递推公式
f[1]=0;
f[i]=(f[i-1]+m)%i; (i>1) (m为相隔的数) (i为现有的人数)
#include<iostream>#include<cmath>#include<cstring>#include<vector>#include<stdlib.h>#include<stdio.h>#include<algorithm>#include<map>#include <set>#include <list>#include <deque>#include<sstream>#include<time.h>#define pi 3.1415926using namespace std;typedef long long ll;const int maxn = 1000000 + 5;const int day =21252;const int mod=1000000007;const ll N=9989910;int main(){ int i; int n,m; int k,j; int t; scanf("%d",&t); while(t--){ scanf("%d",&n); int w=0; for(int i=2;i<=n;i++) { w=(w+(n-i+1))%i; } printf("%d\n",w+1); } return 0;}
阅读全文
0 0
- hdu5463---King's Game
- HDU 5643 King's Game
- BestCoder Round #75 King's Game
- HDU 5643 King's Game 【约瑟夫环】
- HDU 5643:King's Game 约瑟夫问题
- BestCoder Round #75 King's Game
- hdu5463
- BestCoder Round #75 1003 - King's Order & 1004 - King's Game
- HDU 5643 bestcoder Round #75 king's game [威瑟夫问题]
- hdu5643 King's Game (BC#75D)
- hdu 5643 King's Game 数学 约瑟夫问题
- hdu 5643 King's Game (dp约瑟夫环)
- hdu 5643King's Game(约瑟夫游戏,递归)
- hdu5643 King's Game(约瑟夫环+线段树)
- 约瑟夫环问题——(Hdu-5643,King's Game)
- lotus game king
- HDU 5643 King's Game (约瑟夫环问题的变形 递推)
- King's Man
- Mybatis从入门到精通 (一)
- resultMap(12)
- HDU 1069 dp??
- 【linux 命令】环境启动时访问的配置文件顺序
- 欢迎使用CSDN-markdown编辑器
- hdu5463---King's Game
- redis实战之计数信号量(未完)
- Java中的Lambda表达式
- RAISR(Rapid and Accurate Super Image Resolution)
- 过滤器的原理及配置
- 链表划分
- 算法实现:二进制老鼠测毒
- Android 应用内分屏查看(使用fragment)
- Linux服务(DNS)