forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10718.cpp
90 lines (84 loc) · 1.37 KB
/
10718.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
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;
#define MAX(a, b) (a > b ? a : b)
typedef long long ss;
ss A[40], B[40], ind1, ind2, N, L, U, all;
void Binary(ss n, ss a[], ss &ind)
{
ind = 0;
all = 1;
while (n)
{
a[ind++] = n % 2;
if (a[ind - 1] == 0)
all = 0;
n /= 2;
}
}
void Rev(ss a[], ss n)
{
ss temp[40], i, j = 0;
for (i = n - 1; i >= 0; i--)
temp[j++] = a[i];
for (i = 0; i < n; i++)
a[i + 1] = temp[i];
}
int Recur(ss limit, ss level, ss sum)
{
ss x, y, z;
x = limit - level + 1;
y = (ss)pow(2, (double)x) - 1;
if (sum > U)
return 0;
if (sum + y < L)
return 0;
if (level == limit + 1 && sum <= U)
{
printf("%lld\n", sum);
return 1;
}
if (A[level] == 0)
{
z = sum + (ss)pow(2, double(limit - level));
if (Recur(limit, level + 1, z))
return 1;
if (Recur(limit, level + 1, sum))
return 1;
}
if (A[level] == 1)
{
if (Recur(limit, level + 1, sum))
return 1;
z = sum + (ss)(pow(2, (double)(limit - level)));
if (Recur(limit, level + 1, z))
return 1;
}
return 0;
}
void Cal()
{
ss max;
if (U == L)
{
printf("%lld\n", L);
return;
}
for (max = 0; max < 35; max++)
A[max] = 0;
Binary(N, A, ind1);
if (all && N >= U)
{
printf("%lld\n", L);
return;
}
Binary(U, B, ind2);
max = MAX(ind1, ind2);
Rev(A, max);
Recur(max, 1, 0);
}
int main()
{
while (scanf("%lld%lld%lld", &N, &L, &U) == 3)
Cal();
return 0;
}