forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprtition.cc
More file actions
45 lines (42 loc) · 746 Bytes
/
prtition.cc
File metadata and controls
45 lines (42 loc) · 746 Bytes
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
// https://www.codechef.com/JAN18/problems/PRTITION
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main() {
int t; cin >> t;
while (t--) {
int x, n; cin >> x >> n;
ll sum = ll(n) * ll(n + 1) / 2 - x;
if (n <= 3 || sum % 2) {
cout << "impossible\n";
continue;
}
sum /= 2;
vi v(n + 1, 0);
v[x] = 2;
ll s2 = 0;
int i = n;
for (; i >= 0; i--) {
if (i == x) continue;
if (s2 + i > sum) break;
s2 += i;
v[i] = 1;
}
ll d = sum - s2;
if (d != x) v[d] = 1;
else if (d > 2) {
v[d - 1] = 1;
v[1] = 1;
} else {
v[i + 1] = 0;
v[i] = 1;
v[d + 1] = 1;
}
for (int i = 1; i <= n; i++) {
cout << v[i];
}
cout << endl;
}
}