/// Raihan Ruhin /// CSE, Jahangirnagar University. /// Dhaka-Bangladesh. /// id: raihanruhin (topcoder / codeforces / codechef / uva), 3235 (lightoj) /// mail: raihanruhin@ (yahoo / gmail / facebook) /// blog: ruhinraihan.blogspot.com #include<bits/stdc++.h> using namespace std; #define SET(a) memset(a,-1,sizeof(a)) #define CLR(a) memset(a,0,sizeof(a)) #define PI acos(-1.0) #define MOD 1000000007 #define MX 100010 int ring[20], n; bool isPrime[33], taken[20]; void print() { cout<<ring[1]; for(int i=2;i<=n;i++) cout<<" "<<ring[i]; cout<<"\n"; return; } void backTrack(int tot, int lst) //(total taken already, last taken number) { if(tot==n) { if(isPrime[lst+1]) print(); return; } for(int i=2;i<=n;i++) if(!taken[i]) if(isPrime[lst+i]) { taken[i]=1; ring[tot+1]=i; backTrack(tot+1, i); taken[i]=0; } return; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int kk=1, tc, m; string s; isPrime[2]=isPrime[3]=isPrime[5]=isPrime[7]=isPrime[11]=isPrime[13]=isPrime[17]=isPrime[19]=isPrime[23]=isPrime[29]=isPrime[31]=1; while(cin>>n) { CLR(taken); CLR(ring); ring[1]=taken[1]=1;//first circle fixed if(kk>1) cout<<"\n"; cout<<"Case "<<kk++<<":\n"; backTrack(1, 1); //first circle taken } return 0; }
Friday, 8 July 2016
Prime Ring Problem (UVA 524, HDU 1016, UVALive 5270, ZOJ 1457, SCU 1545, Regionals 1996 >> Asia - Shanghai)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment