- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define S 100
- using namespace std;
- int n;
- bool check[23];
- int all[23];
- bool prime[S];
- void primeCheck(){
- int lim = sqrt(S);
- for(int i = 2; i <= lim; i++){
- if(prime[i]){
- for(int j = i*2; j <= S; j+=i){
- prime[j] = false;
- }
- }
- }
- }
- bool sum(int a, int b){
- int c = a+b;
- return prime[c];
- }
- void PrimeRing(int ind){
- int p = all[ind-1]+all[ind-2];
- if(p > 1 && !prime[p])return;
- if(n == ind){
- if(sum(all[0], all[n-1])){
- for(int i = 0; i < n; i++){
- printf("%d", all[i]);
- if(i != n-1)printf(" ");
- }
- puts("");
- }
- return;
- }
- for(int i = 1; i < n; i++){
- if(!check[i]){
- all[ind] = i+1;
- check[i] = true;
- PrimeRing(ind+1);
- check[i] = false;
- }
- }
- }
- int main(){
- int cs = 0;
- memset(prime, true, sizeof(prime));
- primeCheck();
- while(scanf("%d", &n) == true){
- if(cs)puts("");
- memset(check, false, sizeof(check));
- memset(all, 0, sizeof(all));
- printf("Case %d:\n", ++cs);
- all[0] = 1;
- PrimeRing(1);
- }
- return 0;
- }
Thursday, February 25, 2016
UVa - 524 - Prime Ring Problem
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment