标签:
很水的题吧,不过以后注意环这种要考虑头尾情况
为了省力素数表范围打错了,太坑了
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include <queue> #include <stack> #include <algorithm> const int inf = (1<<31)-1; const int MAXN = 2e1; const int MMAXN = 4e1; using namespace std; int vist[MAXN]; int ans[MAXN]; int prime[MMAXN]; int n; void makePrime(){ prime[0] = prime[1] = 1; for(int i=2;i<=sqrt(MMAXN);i++){ if(!prime[i]){ for(int j=2*i;j<MMAXN;j+=i){ prime[j] = 1; } } } } void dfs(int x){ if(x==n){ if(!prime[ans[1]+ans[n]]){ for(int i=1;i<n;i++){ printf("%d ",ans[i]); } cout<<ans[n]<<endl; } return; } for(int i=2;i<=n;i++){ if(!vist[i]&&!prime[i+ans[x]]){ ans[x+1] = i; vist[i] = 1; dfs(x+1); vist[i] = 0; } } } int main() { int cas = 0; makePrime(); /*for(int i=0;i<MMAXN;i++){ cout<<prime[i]<<" "; }*/ while(scanf("%d",&n)!=EOF){ memset(vist,0,sizeof(vist)); vist[1] = 1; ans[1] = 1; printf("Case %d:\n",++cas); dfs(1); cout<<endl; } // cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5437313.html