-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lids.cpp
73 lines (53 loc) · 1.48 KB
/
Lids.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
#include <bits/stdc++.h>
using namespace std;
int64_t memo[10+1][2][10+1][10];
int64_t solve(int p, bool low, bool nonzero, int last_digit, int need, const vector<int> &digits) {
if (p < 0) return need == 0;
if (need < 0) return 0;
auto &ans = memo[p][nonzero][last_digit+1][need];
if (low and ans != -1) return ans;
int64_t ret = 0;
int mx = low ? 9 : digits[p];
for (int d = 0; d <= mx; d++) {
if (d > last_digit and (d!=0 or (d==0 and nonzero)))
ret += solve(p-1, low | (d < digits[p]), nonzero | (d!=0), d, need-1, digits);
ret += solve(p-1, low | (d < digits[p]), nonzero | (d!=0), last_digit, need, digits);
}
return low ? ans = ret : ret;
}
vector<int> get_digits(int x) {
vector<int> ret;
for (;x;x/=10) ret.emplace_back(x%10);
return ret;
}
pair<int, int64_t> solve(int x, int y) {
vector<int64_t> lids(10);
auto dy = get_digits(y);
for (int sz = 1; sz < 10; sz++) {
lids[sz] += solve(dy.size() - 1u, false, false, -1, sz, dy);
}
auto dx = get_digits(x-1);
for (int sz = 1; sz < 10; sz++) {
lids[sz] -= solve(dx.size() - 1u, false, false, -1, sz, dx);
}
for (int i = 9; i >= 1; i--) {
if (lids[i]) {
return {i, lids[i]};
}
}
return {-1,-1};
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(memo, -1, sizeof memo);
int T;
cin >> T;
for (int ti = 1; ti <= T; ti++) {
int x, y;
cin >> x >> y;
auto [mx, qtd] = solve(x, y);
cout << "Case " << ti << ": " << mx << ' ' << qtd << '\n';
}
}
// AC, dynamic programming