-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00524 Prime Ring Problem.cpp
85 lines (85 loc) · 1.66 KB
/
00524 Prime Ring Problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <numeric>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <limits>
#include <sstream>
#include <string>
#include <regex>
#define iter(a,b,c,d) for(a = b; a < c; a+=d)
#define ited(a,b,c,d) for(a = b; a > c; a-=d)
#define li long int
#define uli unsigned li
#define ll long li
#define ull unsigned ll
#define si(a) scanf("%d",&a)
#define sli(a) scanf("%ld",&a)
#define sll(a) scanf("%lld",&a)
#define slu(a) scanf("%llu",&a)
#define sf(a) scanf("%f",&a)
#define slf(a) scanf("%lf",&a)
#define PI 3.1415926
using namespace std;
bool isPrime[35];
int n;
bool used[20];
int ring[20];
void popPrimes()
{
int i,j;
fill(isPrime, isPrime+35, true);
iter(i,2,35,1)
{
if(isPrime[i])
{
iter(j,i*i,35,i)
isPrime[j] = false;
}
}
}
void backtrack(int m)
{
int i;
if(m == n - 1 && isPrime[ring[m]+1])
{
printf("1");
iter(i,1,n,1){ printf(" %d",ring[i]);}
printf("\n");
return;
}
iter(i,((m&1)?1:2),n+1,2)
{
if(!used[i] && isPrime[i+ring[m]])
{
used[i] = true;
ring[m+1] = i;
backtrack(m+1);
used[i] = false;
}
}
}
int main()
{
// freopen("1in.txt", "r", stdin);
// freopen("1out.txt", "w", stdout);
popPrimes();
int ct = 0;
used[1] = true;
while(si(n) == 1)
{
if(ct) printf("\n");
printf("Case %d:\n",++ct);
ring[0] = ring[n] = 1;
backtrack(0);
}
return 0;
}