forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10094.cpp
115 lines (111 loc) · 1.4 KB
/
10094.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
bool check(int a[], int N)
{
static char b[34096];
memset(b, 0, sizeof(b));
for (int i = 0; i < N; i++)
{
if (a[i] < 1 || a[i] > N)
{
return false;
}
if (b[a[i]] & 1)
{
return false;
}
b[a[i]] |= 1;
if (b[i + a[i]] & 2)
{
return false;
}
b[i + a[i]] |= 2;
if (b[a[i] - i + N] & 4)
{
return false;
}
b[a[i] - i + N] |= 4;
}
return true;
}
bool gen(int a[], int N)
{
int m;
m = 0;
for (int i = 2; i <= N; i += 2)
{
a[m++] = i;
}
for (int i = 1; i <= N; i += 2)
{
a[m++] = i;
}
if (m == N && check(a, N))
{
return true;
}
m = 0;
for (int i = 4; i <= N; i += 2)
{
a[m++] = i;
}
if (N >= 2)
{
a[m++] = 2;
}
for (int i = 5; i <= N; i += 2)
{
a[m++] = i;
}
for (int i = 1; i <= 3 && i <= N; i += 2)
{
a[m++] = i;
}
if (m == N && check(a, N))
{
return true;
}
m = 0;
for (int i = 2; i <= N; i += 2)
{
a[m++] = i;
}
for (int i = 3; i >= 1; i -= 2)
if (i <= N)
{
a[m++] = i;
}
for (int i = 7; i <= N; i += 2)
{
a[m++] = i;
}
if (5 <= N)
{
a[m++] = 5;
}
if (m == N && check(a, N))
{
return true;
}
return false;
}
int main()
{
int a[2048], N;
while (scanf("%d", &N) == 1 && N > 0)
{
if (!gen(a, N))
{
printf("Impossible\n");
}
else
{
for (int i = 0; i < N; i++)
{
printf(i == 0 ? "%d" : " %d", a[i]);
}
printf("\n");
}
}
return 0;
}