标签:des style blog http io ar color os sp
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28391 Accepted Submission(s): 12644
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n; int vis[50]; int seq[50]; int prime[100]; bool okp(int x) { int i; for(i=2;i<=sqrt(x);i++) { if(x%i==0) break; } if(i>sqrt(x)) return true; return false; } void prim() { memset(prime,0,sizeof(prime)); int i; for(i=2;i<=50;i++) { if(prime[i]==0) { if(okp(i)) prime[i]=1; } int k=i,tc=2; while(k*tc<=50) { prime[k*tc]=2; tc++; } } } void dfs(int x) { int i; if(x>n) return ; if(x==n&&prime[seq[x]+1]==1) { for(i=1;i<=n;i++) { printf("%d",seq[i]); printf("%c",i==n?‘\n‘:‘ ‘); } } else { for(i=2;i<=n;i++) { if(vis[i]==0&&prime[seq[x]+i]==1) { vis[i]=1; seq[x+1]=i; dfs(x+1); vis[i]=0; } } } } int main() { int cas=1,i,j; prim();//筛法求素数 while(scanf("%d",&n)!=EOF) { printf("Case %d:\n",cas++); //if(n==1) continue; //测试数据中不会出现n==1 memset(vis,0,sizeof(vis)); seq[1]=1; vis[1]=1; dfs(1); printf("\n"); } return 0; }
hdu 1016 Prime Ring Problem(dfs)
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/sola1994/p/4153395.html