-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCC-POSAND.cpp
More file actions
105 lines (99 loc) · 2.32 KB
/
CC-POSAND.cpp
File metadata and controls
105 lines (99 loc) · 2.32 KB
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
/*
This code is written by Shammi Anand
contact : shammianand101@gmail.com, shammianand.me
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define vi vector<int>
#define pii pair<int, int>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define F(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(), a.end()
#define INF 1e9+7
#define nl "\n"
#define w(x) int x; cin>>x; while(x--)
bool isPowerOfTwo (int x) {
return x && (!(x & (x - 1)));
}
template<typename T_vector>
void output(const T_vector &v, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = int(v.size());
for (int i = start; i <= end; i++) {
cout << v[i] << (i == end ? '\n' : ' ');
}
}
void shammi() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main() {
shammi();
w(t) {
int n; cin >> n;
if (isPowerOfTwo(n) && n != 1) {
cout << -1 << nl;
} else {
if (n == 1) {
cout << 1 << nl;
continue;
}
vi powers;
int count = 0;
for (int i = 1; i <= 17; i++) {
if (n / pow(2, i) >= 1) {
count = i;
}
}
// cout << count << nl;
vector<bool> printed(n + 1, false);
bool last_printed_powerof2 = false;
int counter = n, value = n;
vi p(n + 1);
while (counter >= 0) {
if (last_printed_powerof2) {
if (printed[value]) {
p[counter] = value - 1;
printed[value - 1] = true;
value -= 2;
last_printed_powerof2 = false;
} else {
p[counter] = value;
printed[value] = true;
value--;
last_printed_powerof2 = false;
}
} else {
if (count != 0) {
p[counter] = pow(2, count);
printed[pow(2, count)] = true;
count--;
last_printed_powerof2 = true;
} else {
if (printed[value]) {
p[counter] = value - 1;
printed[value - 1] = true;
value -= 2;
last_printed_powerof2 = false;
} else {
p[counter] = value;
printed[value] = true;
value--;
last_printed_powerof2 = false;
}
}
}
counter--;
}
output(p, 1, n);
}
}
return 0;
}