UVA10871 Primed Subsequence欧拉筛法

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10871 Primed Subsequence欧拉筛法相关的知识,希望对你有一定的参考价值。

Given a sequence of positive integers of length n, we define a primed subsequence as a consecutive subsequence of length at least two that sums to a prime number greater than or equal to two.
    For example, given the sequence:
3 5 6 3 8
    There are two primed subsequences of length 2 (5 + 6 = 11 and 3 + 8 = 11), one primed subsequence of length 3 (6 + 3 + 8 = 17), and one primed subsequence of length 4 (3 + 5 + 6 + 3 = 17).
Input
Input consists of a series of test cases. The first line consists of an integer t (1 < t < 21), the number of test cases. Each test case consists of one line. The line begins with the integer n, 0 < n < 10001, followed by n non-negative numbers less than 10000 comprising the sequence. You should note that 80% of the test cases will have at most 1000 numbers in the sequence.
Output
For each sequence, print the ‘Shortest primed subsequence is length x:’, where x is the length of the shortest primed subsequence, followed by the shortest primed subsequence, separated by spaces. If there are multiple such sequences, print the one that occurs first. If there are no such sequences, print ‘This sequence is anti-primed.’.
Sample Input
3
5 3 5 6 3 8
5 6 4 5 4 12
21 15 17 16 32 28 22 26 30 34 29 31 20 24 18 33 35 25 27 23 19 21
Sample Output
Shortest primed subsequence is length 2: 5 6
Shortest primed subsequence is length 3: 4 5 4
This sequence is anti-primed.

问题链接UVA10871 Primed Subsequence
问题简述:(略)
问题分析:素数有关的问题,用欧拉筛法来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

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

/* UVA10871 Primed Subsequence */

#include <bits/stdc++.h>

using namespace std;

// 欧拉筛法
const int N = 10000;
bool isprime[N + 1];
int prime[N / 3], 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;
        }
    }
}

bool pjudge(int x)
{
    if (x <= N) return isprime[x];
    for (int i = 0; i < pcnt; i++)
        if (x % prime[i] == 0) return false;
    return true;
}

int s[N + 1];

int main()
{
    eulersieve();

    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);

        s[0] = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &s[i]);
            s[i] += s[i - 1];
        }

        bool flag = false;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j + i - 1 <= n; j++) {
                int t = s[i + j - 1] - s[j - 1];
                if (pjudge(t)) {
                    flag = true;
                    printf("Shortest primed subsequence is length %d:", i);

                    for (int k = 1; k <= i; k++)
                        printf(" %d", s[j + k - 1] - s[j + k - 2]);
                    printf("\\n");

                    break;
                }
            }
            if (flag) break;
        }
        if (flag == false)
            printf("This sequence is anti-primed.\\n");
    }

    return 0;
}

以上是关于UVA10871 Primed Subsequence欧拉筛法的主要内容,如果未能解决你的问题,请参考以下文章

20160327~20160402

1087 1 10 100 1000

Innovus卡住,提示X Error:

npm insatll 下载依赖总会卡住

2019年2月做题记录

uva是啥意思