zoukankan      html  css  js  c++  java
  • HDU 1016

    不想说这题有多么水了,最简单的DFS题了,但是!我花了N天在杭电用G++交了20多遍一直超时,最后改C++交过了!!

    这是什么情况!!无语了!!

    #define _CRT_SECURE_NO_WARNINGS
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    const int maxn = 1024;
    bool checked[maxn] = { 1, 1 };
    int pri[maxn], tot = 0;
    int n, res[22], kase = 0, vis[22];
    
    void init(){
        for (int i = 2; i < maxn; ++i){
            if (!checked[i]) pri[tot++] = i;
            for (int j = 0; j < tot && pri[j] * i < maxn; ++j){
                checked[pri[j] * i] = 1;
                if (!(i % pri[j])) break;
            }
        }
    }
    
    void DFS(const int d){
        if (d == n){
            if (!checked[1 + res[d - 1]]){
                for (int i = 0; i < n; ++i)
                    printf("%d%c", res[i], i == n - 1 ? '
    ' : ' ');
            }
            return;
        }
        for (int i = 2; i <= n; ++i){
            if (!vis[i] && !checked[i + res[d - 1]]){
                vis[i] = 1;
                res[d] = i; DFS(d + 1);
                vis[i] = 0;
            }
        }
    }
    
    int main(){
        init();
        while (~scanf("%d", &n)) {
            printf("Case %d:
    ", ++kase);
            memset(vis, 0, sizeof(vis));
            res[0] = 1; vis[1] = 1;
            DFS(1);
            printf("
    ");
        }
        return 0;
    }


  • 相关阅读:
    EF 配置实现建表与迁移
    微服务
    CentOS 系统管理与yum软件仓库搭建
    CentOS 网络操作
    CentOS 进程操作
    CentOS 用户/组与权限
    C# 三个Timer
    Entity Framework 小知识(四)
    Entity Framework 索引
    Entity Framework 小知识(三)
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312683.html
Copyright © 2011-2022 走看看