2012-04-13 06:58:28Morris

[UVA] 10871 - Primed Subsequence

Problem B
Primed Subsequence
Input:
standard input
Output: standard output


Primed Sequences

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

 

 

Output for Sample Input

Shortest primed subsequence is length 2: 5 6

Shortest primed subsequence is length 3: 4 5 4

This sequence is anti-primed.



做法 Greedy+判斷質數
優先長度由 2 窮舉到 n
  次基點由 1 窮舉到 n
窮舉 O(n^2) 也會過

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Prime[5500], Pt;
void sieve() {
    Pt = 0;
    char mark[50000] = {};
    int i, j;
    for(i = 2; i < 50000; i++) {
        if(mark[i] == 0) {
            Prime[Pt++] = i;
            for(j = 2; i*j < 50000; j++)
                mark[i*j] = 1;
        }
    }
}
int judgePrime(int n) {
    static int i, sqr;
    sqr = (int)sqrt(n);
    for(i = 0; Prime[i] <= sqr && i < Pt; i++)
        if(n%Prime[i] == 0)
            return 0;
    return 1;
}
int main() {
    sieve();
    int t, n, i, j, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        int A[10000], B[10000];
        for(i = 0; i < n; i++) {
            scanf("%d", &A[i]);
            B[i] = A[i];
            if(i != 0)
                A[i] += A[i-1];
        }
        int flag = 0, tmp;
        for(i = 2; i <= n && !flag; i++) {
            for(j = 0; j <= n-i && !flag; j++) {
                if(j == 0)
                    tmp = A[j+i-1];
                else
                    tmp = A[j+i-1] - A[j-1];
                if(judgePrime(tmp) == 1) {
                    printf("Shortest primed subsequence is length %d:", i);
                    for(k = j; k < j+i; k++)
                        printf(" %d", B[k]);
                    flag = 1;
                    puts("");
                }
            }
        }
        if(!flag)
            puts("This sequence is anti-primed.");
    }
    return 0;
}