POJ-1012
来源:互联网 发布:ipad文献阅读软件 编辑:程序博客网 时间:2024/06/06 09:39
Joseph
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 39717 Accepted: 14849
Description
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
340
Sample Output
530
Source
Central Europe 1995
#include <stdio.h> int main(){ int k, i, bad, last; int num[15]; // 预先把所有结果算出来比等输入后再算要快 for (k = 1; k <= 14; k++) { for (i = k+1;;) { bad = k<<1; last = 0; while (bad != k) { last = (last+i-1)%bad; if (last+1 > k) bad--; else break; } if (bad == k) { num[k] = i; break; } if (i%(k+1)) i += k; else i++; } } while (scanf("%d", &k), k) { printf("%d\n", num[k]); } return 0;}
- POJ 1012
- POJ 1012
- POJ 1012
- POJ 1012
- poj 1012
- POJ 1012
- POJ-1012
- poj 1012
- POJ 1012
- poj 1012
- poj 1012
- poj 1012
- POJ 1012
- POJ 1012
- 约瑟夫环问题 poj 1012 && poj 2244
- POJ 1012 Joseph
- POJ 1012 约瑟夫问题
- POJ 1012 Joseph
- “菜鸟”程序员和“大神”程序员差距在哪里
- 任意进制转换
- POJ-1002
- 筛法求素数
- 内核等待队列(笔记)
- POJ-1012
- eclipselink->JAXB:官网、API、文档
- otp in action 半抄半改的一个demo完成
- 日记-5月17日-专注
- 基本的电路理论知识
- Prim算法求最小生成树
- 第二章 构造和运行模块(笔记)
- jquery获取css中的选择器
- maven权威指南学习(一)