-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingNumbers.cpp
More file actions
51 lines (41 loc) · 1.21 KB
/
CountingNumbers.cpp
File metadata and controls
51 lines (41 loc) · 1.21 KB
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
//Counting Numbers - https://cses.fi/problemset/task/2220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
ll dp[20][11][2][2];
void solve() {
ll a, b;
cin >> a >> b;
auto dfs = [&](auto dfs, string s, int index, int last, int tight, int zero) -> ll {
if (index >= (int)s.size()) {
return 1;
}
if (dp[index][last][tight][zero] != -1) {
return dp[index][last][tight][zero];
}
int bound = (tight ? s[index] - '0' : 9);
ll ans = 0;
for (int digit = 0; digit <= bound; digit++) {
if (digit == last && !zero) continue;
ans += dfs(dfs, s, index + 1, digit, tight && (s[index] - '0') == digit, zero && digit == 0);
}
return dp[index][last][tight][zero] = ans;
};
memset(dp, -1, sizeof(dp));
ll ansHigh = dfs(dfs, to_string(b), 0, 10, 1, 1);
memset(dp, -1, sizeof(dp));
ll ansLow = dfs(dfs, to_string(a - 1), 0, 10, 1, 1);
cout << ansHigh - ansLow << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}