Skip to content

Commit f75c9e3

Browse files
committed
Solved problem 10705 - The Fun Number System from UVA
1 parent 5c0815c commit f75c9e3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

UVA/10705 - The Fun Number System.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

UVA/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
- [10608 - Friends](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1549)
114114
- [10611 - The Playboy Chimp](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1552)
115115
- [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)
116117
- [10717 - Mint](https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1658)
117118
- [10721 - Bar Codes](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1662)
118119
- [10731 - Test](https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&problem=1672)

0 commit comments

Comments
 (0)