UVA11053 POJ2939 HDU1488 Flavius Josephus Reloaded【循环节】

509 篇文章 6 订阅
132 篇文章 1 订阅

Flavius Josephus Reloaded
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2193 Accepted: 818

Description

Flavius Josephus once was trapped in a cave together with his comrade soldiers surrounded by Romans. All of Josephus’ fellow soldiers preferred not to surrender but to commit suicide. So they all formed a circle and agreed on a number k. Every k-th person in the circle would then commit suicide. However, Josephus had different priorities and didn’t want to die just yet. According to the legend he managed to find the safe spot in the circle where he would be the last one to commit suicide. He surrendered to the Romans and became a citizen of Rome a few years later.

It is a lesser known fact that the souls of Josephus and his comrades were all born again in modern times. Obviously Josephus and his reborn fellow soldiers wanted to avoid a similar fiasco in the future. Thus they asked a consulting company to work out a better decision scheme. The company came up with the following scheme:

For the sake of tradition all soldiers should stand in a circle. This way a number between 0 and N − 1 is assigned to each soldier, where N is the number of soldiers.
As changing numbers in the old scheme turned out to be horribly inefficient, the number assigned to a soldier will not change throughout the game.
The consulting company will provide two numbers a and b which will be used to calculate the number of the next soldier as follows: Let x be the number of the current soldier, then the number of the next soldier is the remainder of a · x2 + b mod N.
We start with the soldier with number 0 and each soldier calculates the number of the next soldier according to the formula above.
As everyone deserves a second chance a soldier will commit suicide once his number is calculated for the second time.
In the event that the number of a soldier is calculated for the third time the game will end and all remaining soldiers will surrender.
You are to write a program that given the number of soldiers N and the constants a and b determines the number of survivors.

Input

The input file consists of several test cases. Each test case consists of a single line containing the three integers N (2 ≤ N ≤ 109), a and b (0 ≤ a, b < N) separated by white space. You may safely assume that the first soldier dies after no more than one million (106) steps. The input is terminated by a single number 0 which should not be processed.

Output

For each test case output a single line containing the number of soldiers that survive.

Sample Input

2 1 1
5 1 1
10 3 7
101 9 2
698253463 1 181945480
1000000000 999999999 999999999
0

Sample Output

0
2
4
96
698177783
999999994

Source

Ulm Local 2006

问题链接UVA11053 POJ2939 HDU1488 Flavius Josephus Reloaded
问题简述:(略)
问题分析:循环节问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11053 POJ2939 HDU1488 Flavius Josephus Reloaded */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;
LL n, a, b;
inline LL f(LL next)
{
    return (a * (next * next % n) + b) % n;
}

int main()
{
    while (scanf("%lld%lld%lld", &n, &a, &b) == 3) {
        a %= n;
        b %= n;

        LL h = 0, t = 0;
        do {
            h = f(f(h));
            t = f(t);
        } while (t != h);
        h = 0;
        while (t != h) {
            h = f(h);
            t = f(t);
        }

        int ans = n;
        LL start = h;
        do {
            h = f(h);
            ans--;
        } while (h != start);

        printf("%d\n", ans);
    }

    return 0;
}

AC的C++语言程序(只能在UVA中AC,POJ和HDU中TLE)如下:

/* UVA11053 Flavius Josephus Reloaded */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int main()
{
    LL n, a, b;
    while (scanf("%lld%lld%lld", &n, &a, &b) == 3) {
        a %= n;
        b %= n;

        map<int, int> cnt;
        LL next = 0;
        int ans = n;
        for (; ;) {
            next = (a * (next * next % n) + b) % n;
            if (++cnt[next] == 2) ans--;
            else if(cnt[next] == 3) break;
        }
        printf("%d\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值