-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2071b.cpp
More file actions
78 lines (69 loc) · 1.44 KB
/
2071b.cpp
File metadata and controls
78 lines (69 loc) · 1.44 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
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
bool isPerfectSquare(ll x) {
if (x <= 0)
return false;
ll root = sqrt(x);
return (root * root == x);
}
void solve() {
int n;
cin >> n;
ll total = (ll)n * (n + 1) / 2;
if (isPerfectSquare(total)) {
cout << "-1\n";
return;
}
if (n == 1) {
cout << "-1\n";
return;
}
vector<int> res = {2, 1};
if (n == 2) {
cout << "2 1\n";
return;
}
set<int> avail;
for (int i = 3; i <= n; i++) {
avail.insert(i);
}
ll current_sum = 3;
while (!avail.empty()) {
bool found = false;
for (auto it = avail.begin(); it != avail.end(); ) {
int x = *it;
ll next_sum = current_sum + x;
if (!isPerfectSquare(next_sum)) {
res.push_back(x);
current_sum = next_sum;
it = avail.erase(it);
found = true;
break;
} else {
it++;
}
}
if (!found) {
cout << "-1\n";
return;
}
}
for (int i = 0; i < res.size(); i++) {
cout << res[i];
if (i < res.size() - 1)
cout << ' ';
}
cout << '\n';
}
int main() {
int cases;
cin >> cases;
while (cases--) {
solve();
}
return 0;
}