PCIRCLE - Vòng số nguyên tố

Tác giả: khuc_tuan

Ngôn ngữ: C++

#include "iostream"
#include "stdio.h"

using namespace std;

int n;
int a[22];
bool ngto[100];
bool dd[100];
int socach;

void duyet(int i, bool in) {
	if(in && socach>=10000) return;
	if(i==2*n) {
		if(ngto[a[2*n-1]+a[0]]) {
			++socach;
			if(in && socach<=10000) {
				for(int i=0;i<2*n;++i) printf("%d ",a[i]);
				printf("\n");
			}
		}
		return;
	}
	
	for(int x=1;x<=2*n;++x) if(ngto[x+a[i-1]] && !dd[x]) {
		dd[x] = true;
		a[i] = x;
		duyet(i+1,in);
		dd[x] = false;
	}
}

void init() {
	memset( ngto, true, sizeof(ngto));
	ngto[0] = ngto[1] = false;
	for(int i=2;i<100;++i) {
		if(ngto[i]) {
			for(int j=i+i;j<100;j+=i) ngto[j] = false;
		}
	}
}

int main() {
	scanf("%d",&n);
	a[0] = 1;
	dd[1] = true;
	init();
	duyet(1, false);
	printf("%d\n", socach);
	socach = 0;
	duyet(1, true);
	return 0;
}

Download