【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;
}

 

转载于:https://www.cnblogs.com/YJinpeng/p/5971014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值