#include <iostream>
#include <vector>
#include <cstring>
#include <cassert>
#include <algorithm>
#define long long long
using namespace std;
const int D = 20;
long dp[D][11][11][2][2]; // (number_of_digits, pre_last_digit, last_digit, leading_not_zero, palindrome)
int a[40];
long DP(int n, int y, int x, int z, int pl) {
if (n == 0) return !pl;
long &ans = dp[n][y][x][z][pl];
if (ans != -1) return ans;
ans = 0;
for (int d = 0; d < 10; ++d) {
if (z == 1) {
int p = y - z, q = x - z;
// even-palin or odd-palin
ans += DP(n - 1, x, d + 1, 1, pl | (q == d) | (p == d));
} else ans += DP(n - 1, x, d + (z | (d > 0)), z | (d > 0), pl);
}
return ans;
}
long Compute(long v) {
int n = 0;
while (v) {
a[++n] = v % 10;
v /= 10;
}
reverse(a + 1, a + n + 1);
int pre_last_digit = -1, last_digit = -1, pl = 0;
long ans = 0;
for (int i = 1; i <= n; ++i) {
for (int d = 0; d < a[i]; ++d) {
if (i == 1 && d == 0) {
// leading zero
ans += DP(n - i, 0, 0, 0, 0);
} else if (i == 1) {
// d is a first digit
long pre_ans = ans;;
ans += DP(n - i, 0, d + 1, 1, 0);
// cout << d << " = " << ans - pre_ans << '\n';
} else {
int p = pre_last_digit, q = last_digit;
assert(last_digit != -1);
// even-palin or odd-palin
ans += DP(n - i, last_digit + 1, d + 1, 1, pl | (q == d) | (p == d));
}
}
pl |= (a[i] == last_digit);
pl |= (a[i] == pre_last_digit);
pre_last_digit = last_digit;
last_digit = a[i];
}
return ans;
}
void Brute(int l, int r) {
int ans = 0;
for (int i = l; i <= r; ++i) {
// bool x = (i % 10) | ()
if (i % 10 == i / 100) ++ans;
else if (i / 100 == (i / 10) % 10) ++ans;
else if ((i / 10) % 10 == i % 10) ++ans;
}
cout << "Brute: " << ans << '\n';
}
int main() {
memset(dp, -1, sizeof dp);
long l, r; cin >> l >> r;
// Brute(100, 199);
long y = Compute(r + 1);
long x = Compute(l);
// cout << y << " " << x << '\n';
cout << y - x << '\n';
return 0;
}
Compilation message
numbers.cpp: In function 'long long int Compute(long long int)':
numbers.cpp:49:10: warning: unused variable 'pre_ans' [-Wunused-variable]
49 | long pre_ans = ans;;
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
416 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |