-
Notifications
You must be signed in to change notification settings - Fork 79
/
decibinary_numbers.cpp
88 lines (81 loc) · 1.41 KB
/
decibinary_numbers.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
#include <ios>
#include <iostream>
long long int dp[25][300005] = {};
long long int nm[300005] = {};
long long int cnt(int d, int s)
{
if (d == -1 && s == 0)
return 1;
else if (d == -1)
return 0;
else if (dp[d][s] == -1)
{
dp[d][s] = 0;
for (int i = 0; i <= 9 && (1 << d)*i <= s; i++)
dp[d][s] += cnt(d-1, s-((1 << d)*i));
}
return dp[d][s];
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
for (int i = 0; i < 25; i++)
for (int j = 0; j < 300005; j++)
dp[i][j] = -1;
for (int i = 0; i < 300005; i++)
nm[i] = cnt(24, i);
for (int i = 1; i < 300005; i++)
nm[i] += nm[i-1];
int q, lo, hi, ans;
long long int x;
std::cin >> q;
while (q--)
{
std::cin >> x;
if (x == 1)
std::cout << 0 << '\n';
else
{
lo = 0;
hi = 300004;
while (lo <= hi)
{
int mid = (lo+hi)/2;
if (nm[mid] >= x)
{
ans = mid;
hi = mid-1;
}
else
lo = mid+1;
}
long long int g = x-nm[ans-1];
long long int s = ans;
long long int val;
int d;
for (int i = -1; cnt(i, s) < g; i++)
d = i;
d++;
while (d >= 0)
{
val = 0;
for (int i = 0; i <= 9; i++)
{
if ((s - (1 << d)*i) >= 0)
val += cnt(d-1, s-(1 << d)*i);
if (val >= g)
{
std::cout << i;
g -= val-cnt(d-1, s-(1 << d)*i);
s -= (1 << d)*i;
break;
}
}
d--;
}
std::cout << '\n';
}
}
}