File tree 2 files changed +82
-0
lines changed
2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Idea:
3
+ - Recursion with pruning.
4
+ - We can try to put 0 or 1 in each place in the binary representation
5
+ of the number, but we need to check that if we put 1 and we can not
6
+ reach the number `n` in the future we can skip this branch.
7
+ */
8
+
9
+ #include < bits/stdc++.h>
10
+
11
+ using namespace std ;
12
+
13
+ int const N = 65 ;
14
+ char s[N];
15
+ int t, k, sol[N];
16
+ long long n;
17
+ long long pos[N], neg[N], cpos[N], cneg[N];
18
+
19
+ bool rec (int idx, long long num) {
20
+ if (idx == k) {
21
+ if (n == num) {
22
+ for (int i = 0 ; i < k; ++i)
23
+ printf (" %d" , sol[i]);
24
+ puts (" " );
25
+ return true ;
26
+ }
27
+
28
+ return false ;
29
+ }
30
+
31
+ sol[idx] = 1 ;
32
+ if (s[idx] == ' p' ) {
33
+ if (num + pos[idx] - (cneg[k] - cneg[idx]) > n) {
34
+ sol[idx] = 0 ;
35
+ if (rec (idx + 1 , num))
36
+ return true ;
37
+ } else {
38
+ if (rec (idx + 1 , num + pos[idx]))
39
+ return true ;
40
+ }
41
+ } else {
42
+ if (num - neg[idx] + (cpos[k] - cpos[idx]) < n) {
43
+ sol[idx] = 0 ;
44
+ if (rec (idx + 1 , num))
45
+ return true ;
46
+ } else {
47
+ if (rec (idx + 1 , num - neg[idx]))
48
+ return true ;
49
+ }
50
+ }
51
+
52
+ return false ;
53
+ }
54
+
55
+ int main () {
56
+ scanf (" %d" , &t);
57
+ while (t-- != 0 ) {
58
+ for (int i = 0 ; i < k; ++i)
59
+ pos[i] = neg[i] = 0 ;
60
+
61
+ scanf (" %d" , &k);
62
+ scanf (" %s" , s);
63
+ scanf (" %lld" , &n);
64
+
65
+ for (int i = 0 ; i < k; ++i)
66
+ if (s[i] == ' p' )
67
+ pos[i] = (1llu << ((k - 1 ) - i));
68
+ else
69
+ neg[i] = (1llu << ((k - 1 ) - i));
70
+
71
+ for (int i = 1 ; i <= k; ++i) {
72
+ cpos[i] = pos[i - 1 ] + cpos[i - 1 ];
73
+ cneg[i] = neg[i - 1 ] + cneg[i - 1 ];
74
+ }
75
+
76
+ if (!rec (0 , 0 ))
77
+ puts (" Impossible" );
78
+ }
79
+
80
+ return 0 ;
81
+ }
Original file line number Diff line number Diff line change 113
113
- [ 10608 - Friends] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1549 )
114
114
- [ 10611 - The Playboy Chimp] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1552 )
115
115
- [ 10673 - Play with Floor and Ceil] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1614 )
116
+ - [ 10705 - The Fun Number System] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1646 )
116
117
- [ 10717 - Mint] ( https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1658 )
117
118
- [ 10721 - Bar Codes] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1662 )
118
119
- [ 10731 - Test] ( https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&problem=1672 )
You can’t perform that action at this time.
0 commit comments