【codevs3895】环素数

problem

solution

codes

#include<cstdio>
int n, A[15], isp[50], vis[50], ok = 1;
bool is_prime(int x){
    for(int i = 2; i < x; i++)if(x%i == 0)return false;
    return true;
}
void dfs(int cur){
    if(cur == n && isp[A[0]+A[n-1]] && A[0] == 1){
        for(int i = 0; i < n; i++)printf("%d ", A[i]);
        printf("\n");
        ok = 0;
    }else for(int i = 1; i <= n; i++){
        if(!vis[i] && isp[i+A[cur-1]]){
            A[cur] = i;
            vis[i] = 1;
            dfs(cur+1);
            vis[i] = 0;
        }
    }
}
int main(){
    scanf("%d", &n);
    if(n == 1){ printf("no"); return 0;}
    isp[1] = isp[2] = 1;
    for(int i = 2; i <= 2*n; i++)isp[i] = is_prime(i);
    dfs(0);
    if(ok)printf("no");
    return 0;
}
posted @ 2018-06-03 10:39  gwj1139177410  阅读(114)  评论(0编辑  收藏  举报
选择