#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int dp[20][20][12][12][2];
vector<int> Convert(int x) {
vector<int> digits;
while(x != 0) {
digits.push_back(x % 10);
x /= 10;
}
reverse(digits.begin(), digits.end());
/*for(int i = 0; i < digits.size(); i++) {
cout << digits[i] << " ";
}
cout << "\n";*/
return digits;
}
void Ini() {
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
for(int k = 0; k < 12; k++) {
for(int l = 0; l < 12; l++) {
for(int m = 0; m < 2; m++) {
dp[i][j][k][l][m] = -1;
}
}
}
}
}
}
int DigitDP(vector<int> s, int pos, int last_zero_trail, int second_last, int last, bool edge) {
//cout << "pos = " << pos << ", last_zero_trail = " << last_zero_trail << ", second_last = " << second_last << ", last = " << last << ", edge = " << edge << "\n";
if(pos == s.size()) {
//cout << "the above is returning 1\n";
return 1;
}
if(dp[pos][last_zero_trail][second_last][last][(edge) ? 1 : 0] != -1) {
//cout << "the value for above is already stored = " << dp[pos][last_zero_trail][second_last][last][(edge) ? 1 : 0] << "\n";
return dp[pos][last_zero_trail][second_last][last][(edge) ? 1 : 0];
}
int ans = 0;
if(edge) {
if(last_zero_trail == pos) {
if(s[pos] == 0) {
ans += DigitDP(s, pos + 1, pos + 1, last, 0, true);
}
else {
ans += DigitDP(s, pos + 1, pos + 1, last, 0, false);
}
for(int i = 1; i < s[pos]; i++) {
ans += DigitDP(s, pos + 1, pos, last, i, false);
}
ans += DigitDP(s, pos + 1, pos, last, s[pos], true);
}
else {
for(int i = 0; i < s[pos]; i++) {
if(i != second_last && i != last) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, i, false);
}
else if(i == 0 && i == second_last && last_zero_trail == pos - 1 && i != last) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, i, false);
}
}
if(s[pos] != second_last && s[pos] != last) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, s[pos], true);
}
if(s[pos] == 0 && s[pos] == second_last && last_zero_trail == pos - 1 && s[pos] != last) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, s[pos], true);
}
}
}
else {
if(last_zero_trail == pos) {
ans += DigitDP(s, pos + 1, pos + 1, last, 0, false);
for(int i = 1; i < 10; i++) {
ans += DigitDP(s, pos + 1, pos, last, i, false);
}
}
else {
if(second_last == 0 && last != 0 && last_zero_trail == pos - 1) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, 0, false);
}
for(int i = 0; i < 10; i++) {
if(i != second_last && i != last) {
ans += DigitDP(s, pos + 1, last_zero_trail, last, i, false);
}
}
}
}
//cout << "pos = " << pos << ", last_zero_trail = " << last_zero_trail << ", second_last = " << second_last << ", last = " << last << ", edge = " << edge << "\n";
//cout << "the answer for above is = " << ans << "\n";
return dp[pos][last_zero_trail][second_last][last][(edge) ? 1 : 0] = ans;
}
void Solve() {
int left, right;
cin >> left >> right;
vector<int> l = Convert(left - 1), r = Convert(right);
Ini();
int ans = DigitDP(r, 0, 0, 10, 10, true);
Ini();
//cout << "ans = " << ans << "\n";
//cout << "\n\n\n\n\n\n\n\n\n\n";
if(left != 0) {
ans -= DigitDP(l, 0, 0, 10, 10, true);
}
//cout << "ans2 = " << dp[0][0][10][10][1] << "\n";
cout << ans << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
numbers.cpp: In function 'long long int DigitDP(std::vector<long long int>, long long int, long long int, long long int, long long int, bool)':
numbers.cpp:39:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(pos == s.size()) {
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
8 ms |
1220 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
8 ms |
1228 KB |
Output is correct |
20 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1228 KB |
Output is correct |
2 |
Correct |
10 ms |
1228 KB |
Output is correct |
3 |
Correct |
10 ms |
1228 KB |
Output is correct |
4 |
Correct |
9 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1212 KB |
Output is correct |
6 |
Correct |
3 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1100 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
10 ms |
1224 KB |
Output is correct |
17 |
Correct |
10 ms |
1228 KB |
Output is correct |
18 |
Correct |
12 ms |
1224 KB |
Output is correct |
19 |
Correct |
10 ms |
1228 KB |
Output is correct |
20 |
Correct |
10 ms |
1228 KB |
Output is correct |
21 |
Correct |
10 ms |
1216 KB |
Output is correct |
22 |
Correct |
8 ms |
1228 KB |
Output is correct |
23 |
Correct |
10 ms |
1228 KB |
Output is correct |
24 |
Correct |
12 ms |
1228 KB |
Output is correct |
25 |
Correct |
13 ms |
1224 KB |
Output is correct |
26 |
Correct |
10 ms |
1224 KB |
Output is correct |
27 |
Correct |
10 ms |
1216 KB |
Output is correct |
28 |
Correct |
10 ms |
1240 KB |
Output is correct |
29 |
Correct |
14 ms |
1224 KB |
Output is correct |
30 |
Correct |
9 ms |
1220 KB |
Output is correct |
31 |
Correct |
9 ms |
1172 KB |
Output is correct |
32 |
Correct |
10 ms |
1228 KB |
Output is correct |
33 |
Correct |
9 ms |
1228 KB |
Output is correct |
34 |
Correct |
10 ms |
1228 KB |
Output is correct |
35 |
Correct |
10 ms |
1228 KB |
Output is correct |
36 |
Correct |
9 ms |
1228 KB |
Output is correct |
37 |
Correct |
8 ms |
1216 KB |
Output is correct |
38 |
Correct |
10 ms |
1228 KB |
Output is correct |
39 |
Correct |
10 ms |
1228 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
14 ms |
1100 KB |
Output is correct |
42 |
Correct |
10 ms |
1228 KB |
Output is correct |
43 |
Correct |
11 ms |
1228 KB |
Output is correct |
44 |
Correct |
14 ms |
1232 KB |
Output is correct |
45 |
Correct |
14 ms |
1228 KB |
Output is correct |