#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
struct SOLUTION {
bool vis[30][11][11][2][2];
// memo[index][penultimate][end_digit][tight][notzeros];
ll memo[30][11][11][2][2];
vector<int> digits;
SOLUTION() {
memset(vis, 0, sizeof(vis));
memset(memo, 0, sizeof(memo));
}
ll dp(int index, int penultimate, int end, bool tight, bool notzeros) {
if (index == digits.size()) {
if (notzeros) return 1;
else return 0;
}
if (vis[index][penultimate][end][tight][notzeros])
return memo[index][penultimate][end][tight][notzeros];
ll res = 0;
int max_next = 9;
if (tight) max_next = digits[index];
for (int i = 0; i <= max_next; i++) {
if (i != penultimate && i != end) {
int t_penultimate = end;
int t_end = i;
if (!notzeros && i == 0) t_end = 10;
bool t_tight = tight && (i == max_next);
bool t_notzeros = notzeros || (i > 0);
res += dp(index + 1, t_penultimate, t_end, t_tight, t_notzeros);
}
}
vis[index][penultimate][end][tight][notzeros] = true;
memo[index][penultimate][end][tight][notzeros] = res;
return res;
}
void get_digits(ll n) {
while (n != 0) {
digits.push_back(n%10);
n /= 10;
}
reverse(digits.begin(), digits.end());
}
bool is_palindrome() {
//cout << digits.size() << endl;
bool res = false;
int i = 0;
if (digits.size()>1)
while (i < digits.size() - 1) {
if (digits[i] == digits[i+1]) res = true;
i++;
}
i = 0;
if (digits.size() > 2)
while (i < digits.size() - 2) {
if (digits[i] == digits[i+2]) res = true;
i++;
}
return res;
}
};
int main() {
ll a, b;
SOLUTION l_bound;
cin >> a >> b;
if (a > b) swap(a, b);
l_bound.get_digits(a-1);
ll lres = -1;
// index 0, allow all numbers inc 0, allow all numbers, tight = true
if (a != 0) lres = l_bound.dp(0, 10, 10, true, false);
SOLUTION u_bound;
u_bound.get_digits(b);
ll ures = u_bound.dp(0, 10, 10, true, false);
//cout << lres << " " << ures << endl;
cout << ures - lres << endl;
}
Compilation message
numbers.cpp: In member function 'll SOLUTION::dp(int, int, int, bool, bool)':
numbers.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (index == digits.size()) {
| ~~~~~~^~~~~~~~~~~~~~~~
numbers.cpp: In member function 'bool SOLUTION::is_palindrome()':
numbers.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | while (i < digits.size() - 1) {
| ~~^~~~~~~~~~~~~~~~~~~
numbers.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while (i < digits.size() - 2) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
620 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
748 KB |
Output is correct |
28 |
Correct |
1 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
620 KB |
Output is correct |
30 |
Correct |
1 ms |
620 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
620 KB |
Output is correct |
34 |
Correct |
1 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
748 KB |
Output is correct |
37 |
Correct |
1 ms |
620 KB |
Output is correct |
38 |
Correct |
1 ms |
620 KB |
Output is correct |
39 |
Correct |
1 ms |
620 KB |
Output is correct |
40 |
Correct |
1 ms |
640 KB |
Output is correct |
41 |
Correct |
1 ms |
620 KB |
Output is correct |
42 |
Correct |
1 ms |
620 KB |
Output is correct |
43 |
Correct |
1 ms |
620 KB |
Output is correct |
44 |
Correct |
1 ms |
620 KB |
Output is correct |
45 |
Correct |
1 ms |
620 KB |
Output is correct |