#include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> using namespace std; bool Prime[1000]; int used[1000],n,data[1000]; void test (int k) { for (int i = 2; i <= n; i++) if (used[i] == false && Prime [i+data[k-1]]) { data[k] = i; used[i] = true; if (k == n) { if (Prime [data[k] + data[1]]) { for(int i=1;i<n;i++) printf("%d ",data[i]); printf("%d\n",data[n]); } } else test (k+1); used[i] = false; } } int main() { memset(Prime,true,sizeof(Prime)); for(int i=4;i<1000;i=i+2)Prime[i]=false; for(int i=3;i<1000;i=i+2) { if(Prime[i]) { for(int j=i*i;j<1000;j=j+2*i)Prime[j]=false; } } int y=1; while(scanf("%d",&n)==1){ if(y>1)printf("\n"); printf("Case %d:\n",y++); if(n==1){printf("1\n");continue;} data[1]=1; for(int i=2;i<=n;i++)used[i]=false; used[1]=true; test(2); } return 0; }
Sunday, August 24, 2014
uva - 524 - Prime Ring Problem
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment