【Codevs1080】质数环

http://codevs.cn/problem/1031/

不讲什么,预处理素数+搜索

// <C.cpp> - Sun Oct  9 12:58:23 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=110;
inline int gi() {
    register int w=0,q=0;register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')q=1,ch=getchar();
    while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
    return q?-w:w;
}
bool f[MAXN],u[MAXN];int a[MAXN],n;
void dfs(int d){
    if(n==d){
        if(f[a[d-1]+1]){
            for(int i=0;i<n;i++)printf("%d ",a[i]);
            printf("\n");
        }return;
    }
    for(int i=2;i<=n;i++)
        if(!u[i]&&f[i+a[d-1]]){
            u[i]=1;a[d]=i;
            dfs(d+1);u[i]=0;
        }
}
int main()
{
    freopen("1031.in","r",stdin);
    freopen("1031.out","w",stdout);
    n=gi();
    u[1]=(bool)(a[0]=1);
    for(int i=2;f[i]=1,i<MAXN;i++)
        for(int j=2,to=sqrt(i);j<=to;j++)
            if(i%j==0){f[i]=0;break;}
    dfs(1);
    return 0;
}

 

posted @ 2016-10-17 19:57  _Mashiro  阅读(216)  评论(0编辑  收藏  举报