UVA10015 Joseph's Cousin【筛选法】

78 篇文章 1 订阅
61 篇文章 3 订阅

The Josephs problem is notoriously known. For those who are not familiar with the problem, among n people numbered 1, 2, . . . , n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give the message about the incident.

  Although many good programmers have been saved since Joseph spread out this information, Josephs Cousin introduced a new variant of the malignant game. This insane character is known for its barbarian ideas and wishes to clean up the world from silly programmers. We had to infiltrate some the agents of the ACM in order to know the process in this new mortal game.

  In order to save yourself from this evil practice, you must develop a tool capable of predicting which person will be saved.

The Destructive Process

  The persons are eliminated in a very peculiar order; m is a dynamical variable, which each time takes a different value corresponding to the prime numbers succession (2,3,5,7,...). So in order to kill the i-th person, Josephs cousin counts up to the i-th prime.

Input

It consists of separate lines containing n [1..3501], and finishes with a ‘0’.

Output

The output will consist in separate lines containing the position of the person which life will be saved.

Sample Input

6

0

Sample Output

4


问题链接UVA10015 Joseph's Cousin

问题简述:(略)

问题分析

  先占个位置,不解释。

程序说明:(略)

题记:(略)

参考链接:(略)


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

/* UVA10015 Joseph's Cousin */

#include <iostream>
#include <math.h>
#include <string.h>

const int N = 50000;
const int SQRTN = ceil(sqrt((double) N));
bool isPrime[N + 1];
int prime[N / 5], pcount;

// Eratosthenes筛选法
void esieve(void)
{
    memset(isPrime, true, sizeof(isPrime));

    isPrime[0] = isPrime[1] = false;
    pcount = 0;
    for(int i=2; i<=SQRTN; i++) {
        if(isPrime[i]) {
            prime[pcount++] = i;

            for(int j=i*i; j<=N; j+=i)  //筛选
                isPrime[j] = false;
        }
    }

    for(int i=SQRTN+1; i<=N; i++)
        if(isPrime[i])
            prime[pcount++] = i;
}


using namespace std;

int main()
{
    esieve();

    int n;
    while(cin >> n && n) {
        int ans = 0;

        for(int i=2; i<=n; i++)
            ans = (ans + prime[n - i]) % i;

        cout << ans + 1 << endl;
    }

    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值