UVA914 Jumping Champion【筛选法】

Professor Ma.L. (Math Lover) loves everything related to prime numbers. Remember that a prime is a positive number
bigger than one and only divisible by 1 and itself. He is now working on a property of a set of primes called the jumping
champion. An integer N is called the “jumping champion” if it is the most frequently occurring difference between consecutive primes.
    For example, consider the consecutive primes 2 3 5 7 11. The differences between primes are 1 2 2 4. Therefore, for this set of primes, the jumping champion is exactly 2 (occurring two times).
    He would really like to know for any set of primes what is their corresponding jumping champion. Could you help him?
    Your task is to write a program that, given a lower and an upper bound, calculates the jumping champion of all the primes numbers that are in the defined limits (the upper and lower bound are considered themselves to be inside the limit).
Input
The first line of input contains an integer T which is the number of test cases that follow.
    Each test case is given on a line with two integer numbers L and U (0 ≤ L ≤ U ≤ 1000000), separated by a single space, which represent the lower and upper limits (respectively) to consider.
Output
The output consists of T lines, one for each case.
    The i-th line contains:
    • ‘The jumping champion is NUM’ — if the jumping champion for the i-th case can be found and it is NUM;
    • ‘No jumping champion’ — if no single jumping champion can be found (if there are less than two primes in the interval or if there is more than one difference occurring a maximum number of times)
Sample Input
3
2 11
2 5
30 50
Sample Output
The jumping champion is 2
No jumping champion
The jumping champion is 4

问题链接UVA914 Jumping Champion
问题简述:计算一个区域内连续素数之间的最大值差值,如果有多个或没有则认为没有
问题分析
    使用筛选法解决问题,不解释。这里假定指定范围内相邻素数的差不超过150。
程序说明:(略)
参考链接:(略)
题记:(略)

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

/* UVA914 Jumping Champion */

#include <bits/stdc++.h>

using namespace std;

const int DP = 150;
const int N = 1000005;
bool isprime[N + 1];
int prime[N / 3], cnt[DP], pcnt = 0;
// 欧拉筛
void eulersieve(void)
{
    memset(isprime, true, sizeof(isprime));

    isprime[0] = isprime[1] = false;
    for(int i = 2; i <= N; i++) {
        if(isprime[i])
            prime[pcnt++] = i;
        for(int j = 0; j < pcnt && i * prime[j] <= N; j++) {  //筛选
            isprime[i * prime[j]] = false;
            if(i % prime[j] == 0) break;
        }
    }
 }

int main()
{
    eulersieve();

    int t, l, u;
    scanf("%d", &t);
    while(t--) {
        memset(cnt, 0, sizeof(cnt));

        scanf("%d%d", &l, &u);
        for(int i = 0; i < pcnt; i++) {
            if(prime[i + 1] > u) break;
            if(prime[i] >= l && prime[i + 1] <= u)
                cnt[prime[i + 1] - prime[i]]++;
        }

        int maxk = 0;
        for(int i = 1; i <= DP; i++)
            if(cnt[i] > cnt[maxk]) maxk = i;
        int cnt2 = 0;
        for(int i = 1; i <= DP; i++)
            if(cnt[i] == cnt[maxk]) cnt2++;

        if(maxk < 1 || cnt2 > 1) printf("No jumping champion\n");
        else printf("The jumping champion is %d\n", maxk);
    }

    return 0;
}
发布了2126 篇原创文章 · 获赞 2306 · 访问量 254万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/104578104