http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=464&page=show_problem&problem=4140

因为这个人最终的编号是0(只剩他一个人),
所以我们可以倒着递推:
0 -> (0+k)%2 -> ((0+k)%2+k)%3 -> ...
最后一步改成m即可。

/*0.016s*/

int main()
{
    int n, k, m;
    while (scanf("%d%d%d", &n, &k, &m), n)
    {
        int x = 0;
        for (int i = 2; i < n; i++) 
            x = (x + k) % i;
        x = (x + m) % n;
        printf("%d\n", x + 1);
    }
    return 0;
}

Comments

comments powered by Disqus