-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path10705 - The Fun Number System.cpp
81 lines (68 loc) · 1.56 KB
/
10705 - The Fun Number System.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
/*
Idea:
- Recursion with pruning.
- We can try to put 0 or 1 in each place in the binary representation
of the number, but we need to check that if we put 1 and we can not
reach the number `n` in the future we can skip this branch.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 65;
char s[N];
int t, k, sol[N];
long long n;
long long pos[N], neg[N], cpos[N], cneg[N];
bool rec(int idx, long long num) {
if(idx == k) {
if(n == num) {
for(int i = 0; i < k; ++i)
printf("%d", sol[i]);
puts("");
return true;
}
return false;
}
sol[idx] = 1;
if(s[idx] == 'p') {
if(num + pos[idx] - (cneg[k] - cneg[idx]) > n) {
sol[idx] = 0;
if(rec(idx + 1, num))
return true;
} else {
if(rec(idx + 1, num + pos[idx]))
return true;
}
} else {
if(num - neg[idx] + (cpos[k] - cpos[idx]) < n) {
sol[idx] = 0;
if(rec(idx + 1, num))
return true;
} else {
if(rec(idx + 1, num - neg[idx]))
return true;
}
}
return false;
}
int main() {
scanf("%d", &t);
while(t-- != 0) {
for(int i = 0; i < k; ++i)
pos[i] = neg[i] = 0;
scanf("%d", &k);
scanf("%s", s);
scanf("%lld", &n);
for(int i = 0; i < k; ++i)
if(s[i] == 'p')
pos[i] = (1llu << ((k - 1) - i));
else
neg[i] = (1llu << ((k - 1) - i));
for(int i = 1; i <= k; ++i) {
cpos[i] = pos[i - 1] + cpos[i - 1];
cneg[i] = neg[i - 1] + cneg[i - 1];
}
if(!rec(0, 0))
puts("Impossible");
}
return 0;
}