uva 1394 - And Then There Was One

点击打开链接uva 1394

思路: 数学递推
分析:
1 题目是一道变形的约瑟夫环变形问题
2 网上看到一篇很好的数学递推法
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。

编号0-(n-1)是有意义的,因为要模n,所以用0-(n-1)更好操作

我们知道第一个人(编号一定是(m-1) mod n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 ... n-2,n-1,0,1,2,... k-2
并且从k开始报0。
现在我们把他们的编号做一下转换:
k --> 0
k+1 --> 1
k+2 --> 2
...
...
k-2 --> n-2
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x'=(x+k) mod n

f[n]=(f[n-1]+k)%n,f[1]=0;  f[i]表示有i个人时,最后胜利者编号

---------------

现在这个问题是从m开始,即是首先(m-1)编号的人出去。。然后就和普通约瑟夫环一样了。

故只要我们f[n]=(f[n-1]+m)%n单独算就行了。其他f[i]=(f[i-1]+k)%i;(1<i<n);

代码:

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

const int MAXN = 10010;
int n , m , k , dp[MAXN];

int main(){
    while(scanf("%d%d%d" , &n,&k,&m) && n+m+k){
         dp[1] = 0;
         for(int i = 2 ; i < n ; i++)
             dp[i] = (dp[i-1]+k)%i;
         dp[n] = (dp[n-1]+m)%n;
         printf("%d\n" , dp[n]+1);
    }
    return 0;
}
时间: 2024-10-02 18:48:42

uva 1394 - And Then There Was One的相关文章

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.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic

UVA 之401 - Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from ri