UVA10930 A-Sequence【序列】

For this problem an A-sequence is a sequence of positive integers ai satisfying 1 ≤ a1 < a2 < a3 < . . . and every ak of the sequence is not the sum of two or more distinct earlier terms of the sequence.

  You should write a program to determine if a given sequence it is or it is not an A-sequence.

Input

The input consists of a set of lines, each line starts with an integer 2 ≤ D ≤ 30 that indicates the number of integers that the current sequence has. Following this number there is the sequence itself. The sequence is composed by integers, each integer is greater than or equal to 1 and less than or equal to 1000. The input is terminated by enf of file (EOF).

Output

For each test case in the input you should print two lines: the first line should indicate the number of the test case and the test case itself; in the the second line you should print ‘This is an A-sequence.’, if the corresponding test case is an A-sequence or ‘This is not an A-sequence.’, if the corresponding test case is not an A-sequence.

Sample Input

2 1 2

3 1 2 3

10 1 3 16 19 25 70 100 243 245 306

Sample Output

Case #1: 1 2

This is an A-sequence.

Case #2: 1 2 3

This is not an A-sequence.

Case #3: 1 3 16 19 25 70 100 243 245 306

This is not an A-sequence.

问题链接UVA10930 A-Sequence

问题简述:(略)

问题分析

  这个问题,看似问的是给定的序列元素ak不是之前的任何两个数或多个元素之和,然而是有陷阱的。实际是判定一个序列是否为B1-Sequence,要根据B1-Sequence的定义来做才行。

  根据题意序列的元素都大于等于1并且是升序的,才是B2-Sequence。所以,要先做这个判定。

  然后,再看序列元素ak不是之前的任何两个数或多个元素之和。程序中用枚举法来实现。

程序说明:(略)

题记:(略)

参考链接:(略)

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

/* UVA10930 A-Sequence */

#include <bits/stdc++.h>

using namespace std;

const int D = 30;
const int N = 1000;
int a[D];
int cnt[N * D + 1];

int main()
{
    int d, caseno = 0;
    while(~scanf("%d", &d)) {
        int sum = 0;
        for(int i = 0; i < d; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
        }

        bool flag = true;
        if(a[0] < 1)
            flag = false;
        for(int i = 1; flag && i < d; i++)
            if(a[i - 1] >= a[i])
                flag = false;

        if(flag) {
            memset(cnt, 0, sizeof(cnt));
            cnt[0] = 1;
            for(int i = 0; i < d; i++) {
                if(cnt[a[i]]) {
                    flag = false;
                    break;
                }
                for(int j = sum; j >= a[i]; j--)
                    if(cnt[j - a[i]])
                        cnt[j] = 1;
            }
        }

        printf("Case #%d:", ++caseno);
        for(int i = 0; i < d; i++)
            printf(" %d", a[i]);
        printf("\nThis is %san A-sequence.\n", flag ? "" : "not ");
    }

    return 0;
}

猜你喜欢

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