poj1012 Joseph

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 48916   Accepted: 18476

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.

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

3
4
0

Sample Output

5
30

Source

Central Europe 1995

题目大意:约瑟夫问题,有2k个人组成一个环,前k个是好人,后k个是坏人,求最小的m,使得每数m个人淘汰一个人,前k个被淘汰的都是坏人。

思路:模拟题,约瑟夫问题的加强版。因为k不大,我们可以采用试探法。从m=1开始代入计算,直到找到符合条件的m为止。该m即是最小的m。模拟的思路,思考了好久终于理清了。我们把全部人编号,0至k-1为好人,k止2k-1为坏人。每次数到第m个人出局。假设现在第i次杀人结束了,杀掉的人的编号是t,下一次杀人的编号是多少?如果杀掉的是好人,我们很容易确定,编号即为(t+m-1)%(n-i+1)。而如果杀掉的是坏人,则有两种情况,编号比t大或编号比t小,这给我们的编程带来了困难。要求每次杀掉的都是坏人,非常不好求。我们可以换一个思路,只要每次杀掉的不是好人就好。那么,每次被杀掉的人的编号只要小于k就行。而后面的坏人的编号即使混乱了也不要紧,因为只要把大于等于k的人都看成是坏人即可,不需要知道他们的编号。这样,我们就可以一轮轮地模拟下去。

 1 /*
 2  * Author:  Joshua
 3  * Created Time:  2015年01月31日 星期六 20时50分27秒
 4  * File Name: poj1012.cpp
 5  */
 6 #include<cstdio>
 7 #include<iostream>
 8 #include<cstring>
 9 #define maxn 30
10 int k,f[maxn];
11
12 void cal(int x)
13 {
14     int e[maxn],m=1,n=2*x;
15     bool flag;
16     e[0]=0;
17     while (1)
18     {
19         flag=true;
20         for (int i=1;i<=x;++i)
21         {
22             e[i]=(e[i-1]+m-1)%(n-i+1);
23             if (e[i]<x)
24             {
25                 ++m;
26                 flag=false;
27                 break;
28             }
29         }
30         if (!flag) continue;
31         f[x]=m;
32         break;
33     }
34 }
35
36 int main()
37 {
38     memset(f,0,sizeof(f));
39     while (scanf("%d",&k)&& k>0)
40     {
41         if (f[k]==0) cal(k);
42         printf("%d\n",f[k]);
43     }
44     return 0;
45 }
时间: 2024-10-21 00:27:59

poj1012 Joseph的相关文章

POJ 1781 In Danger Joseph环 位运算解法

Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" > 具体具体解析请看大师Knuth的Concrete m

Joseph

public class Joseph { /** author: acme* date: 2017-1-26* blogs: http://blog.csdn.net/qq_18297675*/ public static void main(String[] args) { Joseph j = new Joseph(); j.print(41, 3); System.out.println(j.getLastOne(41, 3)); } private class Node { //节点类

UVA305 - Joseph(数论 + 打表)

UVA305 - Joseph(数论 + 打表) 题目链接 题目大意:约瑟夫环问题:n个人围成一圈,每次都淘汰第m个人,问最后一个幸存下来的人的编号. 这题的意思有点不一样,它规定前面的k个人是好人,后面的k个人是坏人(2 ? k形成环).问最小的m是多少,可以先把后面的k个坏人淘汰再淘汰好人. 解题思路:这题有个递推式:ai = (ai - 1 + m - 1) % (2 ? k - (i - 1)) ai表示第i次淘汰的人的编号.后面取余的是剩下的人数. 注意这题的k仅仅可能有14种,可是測

POJ 2800 Joseph’s Problem 数论找规律

Description 求 Input 两个整数n和k(1<=n,k<=1e9) Output 输出 Sample Input 5 3 Sample Output 7 暴力超时,这样就打下表找下余数的规律.输入100,27,一下子就可以看出来,倒着的看,是一段一段的等差序列. 例如100 25 除数 1    2    3    4    5    6    7    8    9     10     11    12   13 14  15  ......25   26........ 商

POJ 1012 Joseph

Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 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,

HDU 1443 Joseph

Problem 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 th

UVA 1363 Joseph&#39;s Problem

https://vjudge.net/problem/UVA-1363 n 题意:求 Σ  k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k-i*p-p = k%i-p 所以 商相同时,余数为等差数列 我不知道为什么交到vjudge一直WA,网上搜的题解交上去也WA #include<cmath> #include<cstdio> using namespace std; int main() { int n,k,i,j,

hdu 1443 Joseph (约瑟夫环)

Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2240    Accepted Submission(s): 1361 Problem Description The Joseph's problem is notoriously known. For those who are not familiar with the

Joseph(约瑟夫环)

Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2094    Accepted Submission(s): 1273 Problem Description The Joseph's problem is notoriously known. For those who are not familiar with the