-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathbathroom_stalls.c
More file actions
54 lines (48 loc) · 979 Bytes
/
bathroom_stalls.c
File metadata and controls
54 lines (48 loc) · 979 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
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned long left;
unsigned long right;
} stalls;
stalls place_one(unsigned long N);
stalls solve(unsigned long N, unsigned long K);
int main()
{
stalls s;
unsigned T, t;
unsigned long N, K;
scanf("%u", &T);
for (t=1; t<=T; t++) {
scanf("%lu %lu\n", &N, &K);
s = solve(N, K);
if (s.right < s.left) {
printf("CASE #%u: %lu %lu\n", t, s.left, s.right);
} else {
printf("CASE #%u: %lu %lu\n", t, s.right, s.left);
}
}
return 0;
}
stalls place_one(unsigned long N) {
stalls ret;
if (N%2 == 0) {
ret.left = N/2 - 1;
ret.right = N/2;
} else {
ret.left = ret.right = (N - 1)/2;
}
return ret;
}
stalls solve(unsigned long N, unsigned long K) {
stalls ret = place_one(N);
while (K > 1) {
if (K%2 == 0) {
ret = place_one(ret.right);
K /= 2;
} else {
ret = place_one(ret.left);
K = (K - 1)/2;
}
}
return ret;
}