uva 305 Joseph

点击打开链接uva 305

思路: 数学+打表
分析:
1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号
2 这一题是前k个人是好人,后面k个是坏人。现在要求最小的m使得没有一个好人被踢出去的情况下k个坏人都被踢出
3 按照传统的方法来分析的话,n个人的编号从0~n-1
   第一次  a[1] = (m-1)%n; // 这里由于人的编号是0~n-1
   第二次  a[2] = (a[1]+m-1)%(n-1);
   第i次     a[i] = (a[i-1]+m-1)%(n-i+1);
   那么我们可以知道每次的删除的人的编号,由于k最大14所以我们可以先打表找到1~14的解,然后输出即可。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int solve(int k){
    int pre;
    int tmp = k+1;
    int n = 2*k;
    while(tmp){
        if((tmp-1)%n >= k && (tmp-1)%n < 2*k){
            pre = (tmp-1)%n;
            int i;
            for(i = 2 ; i <= k ; i++){
               int x = (pre+tmp-1)%(n-i+1);
               if(x < k || x >= 2*k)
                   break;
               else
                   pre = x;
            }
            if(i == k+1)
                return tmp;
        }
        tmp++;
    }
}

int main(){
    int k , ans[20];
    for(int i = 1 ; i < 15 ; i++)
        ans[i] = solve(i);
    while(scanf("%d" , &k) && k)
        printf("%d\n" , ans[k]);
    return 0;
}
时间: 2024-08-01 04:58:44

uva 305 Joseph的相关文章

UVa 216:Getting in Line, 暴力与回溯

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=152 题目类型: 暴力,回溯法 题目: Computer networking requires that the computers in the network be linked. This problem considers a ``lin

UVa 11210 Chinese Mahjong :模拟&amp;amp;枚举&amp;amp;回溯

11210 - Chinese Mahjong Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2151 Mahjong () is a game of Chinese origin usually played by four persons with tiles resembling dominoes and beari

UVa 369 Combinations:如何用double算组合数

369 - Combinations Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=305 Computing the exact number of ways that N things can be taken M at a time can be a

UVa 216 Getting in Line:全排列&amp;amp;枚举

216 - Getting in Line Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=152 Computer networking requires that the computers in the network be linked. This problem considers

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.