• POJ2034 Antiprime Sequences dfs


    筛出素数然后直接暴力即可。

    代码如下:

    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #define MAXN 10000
    using namespace std;
    
    int N, M, d, p[600005];
    int hash[10005], path[10005];
    
    void pre()
    {
        int k;
        for (int i = 4; i <= MAXN; i += 2) {
            p[i] = 1;
        }    
        for (int i = 3; i <= 105; i += 2) {
            if (!p[i]) {
                k = 2 * i;     
                for (int j = i * i; j <= MAXN; j += k) {
                    p[j] = 1;
                }
            }
        }
    }
    
    bool dfs(int x, int step)
    {
        path[step] = x;
        if (step >= 2) { 
            int sum = path[step], k = step > d ? d : step;
            for (int i = step-1; i > step-k; --i) {
                sum += path[i];
                if (!p[sum]) {
                    return false;
                }
            }
            if (step == M-N+1) {
                for (int i = 1; i <= M-N+1; ++i) {
                    printf(i == 1 ? "%d" : ",%d", path[i]);
                }
                puts("");
                return true;
            }
        }
        for (int i = N; i <= M; ++i) {
            if (!hash[i]) {
                hash[i] = 1;
                if (dfs(i, step + 1)) {
                    return true;
                }
                hash[i] = 0;
            }
        }
        return false;
    }
    
    int main()
    {
        int flag;
        pre(); 
        while (scanf("%d %d %d", &N, &M, &d), N|M|d) {
            if (N > M) {
                int t = N; 
                N = M;
                M = t;
            }
            flag = 0;
            memset(hash, 0, sizeof (hash));
            for (int i = N; i <= M; ++i) {
                hash[i] = 1;
                if (dfs(i, 1)) {
                    flag = 1;
                    break;
                }
                hash[i] = 0;
            }
            if (!flag) {
                puts("No anti-prime sequence exists.");
            }
        }
        return 0;    
    }
  • 相关阅读:
    投票协议:二进制表示方法
    投票协议:构建和解析协议消息
    多任务处理:多线程
    通信:成帧与解析
    通信:组合输入输出流
    跨越FSO WSH写文件
    MS07004漏洞
    解决ASP(图像)上传漏洞的方法
    让硬件入侵不再是神话
    解决ASP(图像)上传漏洞的方法
  • 原文地址:https://www.cnblogs.com/Lyush/p/2591023.html
Copyright © 2020-2023  润新知