include iostream include vector include cmath inline int dec int int s

 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
#include <iostream>
#include <vector>
#include <cmath>
inline int dec (int l, int r, std::vector<int>& v);
inline bool check (int l, int r, std::vector<int>& v, std::vector<int>& gist);
inline void gist_update(int l, int r, std::vector<int>& v, std::vector<int>& gist);
void gist_init(int n, std::vector<int>& gist);
void v_init (int n, std::vector<int>& gist, std::vector<int>& v);
void v_build (int n, std::vector<int>& gist, std::vector<int>& v);
int main()
{
std::ios_base::sync_with_stdio(false);
int n;
std::cin >> n;
int pow2n = std::pow(2, n);
std::vector<int> gist(2*pow2n, 0);
gist_init(n, gist);
std::vector<int> v;
v_init(n, gist, v);
v_build(n, gist, v);
for(int i = 0; i < v.size(); ++i)
std::cout << v[i];
std::cout << std::endl;
return 0;
}
inline int dec(int l, int r, std::vector<int>& v) {
int num = 0;
for(int i = l; i <= r; ++i)
num = 2*num + v[i];
return num;
}
void gist_init(int n, std::vector<int>& gist) {
int pow2n = std::pow(2, n);
int k = 2;
int jlim = 1;
for(int i = 1; i <= n; ++i) {
pow2n /= 2;
jlim *= 2;
for(int j = 0; j < jlim; ++j)
gist[k++] = pow2n;
}
}
void v_init(int n, std::vector<int>& gist, std::vector<int>& v) {
for(int i = 0; i < n; ++i)
v.push_back(0);
int igist = 1;
for(int i = 1; i <= n; ++i) {
igist *= 2; // igist = pow(2, i)
gist[igist] -= (n+1-i);
}
}
void v_build(int n, std::vector<int>& gist, std::vector<int>& v) {
int pow2n = std::pow(2, n);
for(int i = 0; i < pow2n-n; ++i) {
v.push_back(0);
if( !check(v.size()-n, v.size()-1, v, gist) )
v[v.size()-1] = 1;
else
gist_update(v.size()-n, v.size()-1, v, gist);
}
for(int i = 1; i < n; ++i)
v.push_back(0);
}
inline bool check(int l, int r, std::vector<int>& v, std::vector<int>& gist) {
int pow2n = 1;
int dec = 0;
for(int i = r; i >= l; --i) {
dec += pow2n * v[i]; //dec = dec(i, r, v)
pow2n *= 2; // pow2n = pow(2, (r-i+1))
if(gist[pow2n + dec] == 0)
return false;
}
return true;
}
inline void gist_update(int l, int r, std::vector<int>& v, std::vector<int>& gist) {
int pow2n = 1;
int dec = 0;
for(int i = r; i >= l; --i) {
dec += pow2n * v[i]; //dec = dec(i, r, v)
pow2n *= 2; // pow2n = pow(2, (r-i+1))
--gist[pow2n + dec];
}
}