码迷,mamicode.com
首页 > 其他好文 > 详细

【Codevs1080】质数环

时间:2016-10-17 20:15:59      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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

 

【Codevs1080】质数环

标签:

原文地址:http://www.cnblogs.com/YJinpeng/p/5971014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!