Poj 2034——Anti-prime Sequences

酶和ATP 2022年05月28日 565次浏览

还不解封,人要疯。
Dfs 暴力枚举每一位,似乎没什么好办法,具体看注释吧。

Poj 2034

Description

Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement of these integers so that each adjacent pair of integers sums to a composite (non-prime) number. For example, if n = 1 and m = 10, one such anti-prime sequence is 1,3,5,4,2,6,9,7,8,10. This is also the lexicographically first such sequence.
给定一个连续的整数序列n,n+1,n+2,…,m,反素数序列是这些整数的重新排列,使得每个相邻的整数对和为一个合成(非素数)数。例如,如果n=1,m=10,则一个这样的反素数序列是1,3,5,4,2,6,9,7,8,10。这也是字典顺序上第一个这样的序列。
We can extend the definition by defining a degree danti-prime sequence as one where all consecutive subsequences of length 2,3,...,d sum to a composite number. The sequence above is a degree 2 anti-prime sequence, but not a degree 3, since the subsequence 5, 4, 2 sums to 11. The lexicographically .rst degree 3 anti-prime sequence for these numbers is 1,3,5,4,6,2,10,8,7,9.
我们可以通过定义阶反素数序列来扩展该定义,其中长度为2,3,…,d的所有连续子序列和为一个合成数。上述序列是2次反素数序列,但不是3次反素数序列,因为子序列5、4、2之和为11。这些数的第一个3次反素数序列的词典顺序是1,3,5,4,6,2,10,8,7,9。

INPUT

Input will consist of multiple input sets. Each set will consist of three integers, n, m, and d on a single line. The values of n, m and d will satisfy 1 <= n < m <= 1000, and 2 <= d <= 10. The line 0 0 0 will indicate end of input and should not be processed.
输入将由多个输入集组成。每组由三个整数组成,n、m和d,放在一行。n、m和d的值将满足1 <= n < m <= 1000,和2 <= d <= 10。0 0 0将表示输入结束,不应该被处理。

Output

For each input set, output a single line consisting of a comma-separated list of integers forming a degree danti-prime sequence (do not insert any spaces and do not split the output over multiple lines). In the case where more than one anti-prime sequence exists, print the lexicographically first one (i.e., output the one with the lowest first value; in case of a tie, the lowest second value, etc.). In the case where no anti-prime sequence exists, output
No anti-prime sequence exists.
对于每个输入集,输出一个由逗号分隔的整数列表组成的单行,形成一个度数的反素数序列(不要插入任何空格,不要将输出分成多行)。在存在多个反素数序列的情况下,打印按字母顺序排列的第一个序列(即输出第一个数值最低的序列;在出现并列的情况下,输出第二个数值最低的序列,等等)。在不存在反素数序列的情况下,输出
不存在反素数序列。

Sample Input

1 10 2
1 10 3
1 10 5
40 60 7
0 0 0

Sample Output

1,3,5,4,2,6,9,7,8,10
1,3,5,4,6,2,10,8,7,9
No anti-prime sequence exists.
40,41,43,42,44,46,45,47,48,50,55,53,52,60,56,49,51,59,58,57,54

代码

#include <iostream>
using namespace std;

const int N = 10000 + 10;

int n, m, k;
int a[N];
bool vis[N];

int primes[N], cnt;
bool st[N];

// 线性筛筛素数
void get_primes() {
    for (int i = 2; i <= N; i++) {
        if (!st[i]) primes[cnt++] = i;
        for (int j = 0; primes[j] <= N / i; j++) {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}
// 判断第 x 位上放 y 这个数字后是否还是反素数序列
bool check(int x, int y) {
    // 如果是第一位,那么不测
    if (x == 0) return true;
    // 只需检测前 k 个数的合
    int sum = y;
    for (int i = x - 1; x - i < k && i > 0; i--) {
        sum += a[i];
        // 只要在阶数范围内,有一个连续的合是素数就不是反素数序列
        if (!st[sum]) return false;
    }
    return true;
}

// x 表示当前判断到第几位
bool dfs(int x) {
    // 如果最后一位,则返回true
    if (x == n - m + 1) return true;
    // 如果没用过这个数字且放进去是反素数序列
    for (int i = m; i <= n; i++) {
        if (!vis[i] && check(x, i)) {
            a[x] = i;
            vis[i] = true;
            if (dfs(x + 1)) return true;

            vis[i] = false;
        }
    }
    return false;
}

int main() {
    get_primes();
    while (scanf("%d%d%d", &m, &n, &k) && (m + n + k)) {
        memset(vis, 0, sizeof(vis));
        if (dfs(0)) {
            for (int i = 0; i < n - m; i++) printf("%d,", a[i]);
            printf("%d\n", a[n - m]);
        } else {
            printf("No anti-prime sequence exists.\n");
        }
    }
    return 0;
}

参考链接,大概看了下似乎确实没有非暴力法:
1.CSDN
2.luogu