国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU 5643 King's Game (約瑟夫環問題的變形 遞推)

2019-11-09 20:33:17
字體:
來源:轉載
供稿:網友

大體題意:

有n 個人,進行比賽,第一輪比賽 從1開始報數,報到1的出局,第二輪報2,,,,  問最后誰沒出局?

思路:

一看就是約瑟夫環問題的變型了。

在簡單記錄一下思考的過程吧:

n 個人 編號為0,1,2,,,,,n-1.

假設這一局k-1 出局了。

那么重新編號:

k  k+1 k+2,,,, n-1  0 1   k-2

0  1     2                           n-2

這n -2 個人進行比賽。

那么我們給他變回去即可!

怎么變呢? 顯然是(x + k) % n = f[n]

但是這里k 是變化的,當i 為2 時  只有兩個人  顯然報數是 n-i+1.

詳細見代碼:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int f[5007];int main(){    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        f[1] = 0;        for (int i = 2; i <= n; ++i){            f[i] = (f[i-1] + n-i+1) % i;        }        PRintf("%d/n",f[n]+1);    }    return 0;}

King's Game

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 703    Accepted Submission(s): 390Problem DescriptionIn order to remember history, King plans to play losephus problem in the parade gap.He calls n(1≤n≤5000) 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 number 2 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 number 3 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 number n?1 is out.And the last man is survivor. Do you know the label of the survivor? InputThe 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 integer n, representing the number of players. OutputOutput 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. SourceBestCoder Round #75 Recommendwange2014   |   We have carefully selected several similar problems for you:  6014 6013 6012 6011 6010  


上一篇:poj2027

下一篇:C# Queue使用

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江川县| 芷江| 广德县| 射阳县| 独山县| 罗江县| 仁寿县| 潞西市| 沈丘县| 琼结县| 新巴尔虎右旗| 庐江县| 文成县| 海宁市| 安龙县| 富宁县| 石阡县| 全州县| 夏津县| 滦南县| 蓝山县| 五原县| 克山县| 乐昌市| 科技| 福州市| 鄄城县| 常宁市| 大足县| 海原县| 娄烦县| 张家口市| 进贤县| 岑溪市| 太仓市| 沾益县| 罗城| 常德市| 贞丰县| 青神县| 嘉鱼县|