• POJ 2939 Flavius Josephus Reloaded(Hash)


    Flavius Josephus Reloaded

    Time Limit: 2000MS

     

    Memory Limit: 65536K

    Total Submissions: 1728

     

    Accepted: 651

    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 ≤ ab < 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

     解题报告:这道题的题意有许多人,按照一定的规则除掉人(有点类似约瑟夫)但是点到两次时退出,当店到三次时(此时这个人已退出),游戏结束,问最后剩余多少人!思路利用哈希,这是上周六内部测试的一道题,队友做的,现在写的也是参考其他队的队友写的;

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAX = 1000007;
    struct Hash//利用链地址法建立哈希表
    {
    int value;
    int count;
    Hash *next;
    }*p[MAX];
    Hash hash[MAX];
    __int64 N, a, b, ans, num;
    bool Judge(__int64 x, __int64 y)
    {
    Hash *q;
    int flag = 0;
    for (q = p[x]; q != NULL; q = q->next)
    {
    if (q->count == y)
    {
    q->value ++;
    if (q->value == 2)//若点到两次就加1, 说明自杀了,
    {
    ans ++;//自杀人数
    }
    else if (q->value == 3)//若一个人点到三次游戏结束
    {
    return true;
    }
    flag = 1;
    break;
    }
    }
    if (!flag)//若这个人是第一次点到时
    {
    q = &hash[num ++];//哈希链长度增加一,静态分配内存
    q->value = 1;
    q->count = y;
    q->next = p[x];
    p[x] = q;
    }
    return false;
    }
    int main()
    {
    __int64 temp, x, y;
    while (scanf("%I64d", &N) != EOF && N)
    {
    scanf("%I64d%I64d", &a, &b);
    num = 0;
    memset(p,0,sizeof(p));
    ans = 0;
    temp = b % N;
    while (1)
    {
    y = temp % MAX;
    if (Judge(y, temp))
    {
    break;
    }
    x = temp % N;
    temp = (((a % N) * ((x * x) % N)) % N + (b % N)) % N;
    }
    printf("%I64d\n", N - ans);
    }
    }



  • 相关阅读:
    linux系统如何发送邮件
    zabbix监测图形界面显示方框乱码解决方法
    Eclipse C++的配置问题launch failed binary not found
    Cpu表现出正弦曲线
    让cpu跑到100%的bat文件
    进程僵死
    华为一些笔试题~~~~零散总结
    数据库~~~投影与除操作
    C++连接Mysql数据库操作
    微软面试题目及答案
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2420095.html
Copyright © 2020-2023  润新知