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(1n5000) soldiers, counterclockwise in a circle, in label 1,2,3...n.

The first round, the first person with label 1 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 numbern1 is out.

And the last man is survivor. Do you know the label of the survivor?
 

Input
The first line contains a number T(0<T5000), the number of the testcases.

For each test case, there are only one line, containing one integer n, representing the number of players.
 

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;}




原创粉丝点击