#include <bits/stdc++.h>
using namespace std;
long long A, B;
int a[20], b[20];
long long dp[20][2][3][100];
long long calc(int x[]) {
// dp[pos][ok][contains how many digits?][prv]
memset(dp, 0, sizeof dp);
dp[0][0][0][0] = 1;
for (int i = 1; i <= 18; ++i) {
for (int ok = 0; ok < 2; ++ok) {
for (int cnt = 0; cnt < 3; ++cnt) {
for (int prv = 0; prv < 100; ++prv) { // prv contains one or two digits
if (!dp[i-1][ok][cnt][prv]) continue;
int mx = 9; if (!ok) mx = x[i];
for (int dig = 0; dig <= mx; ++dig) {
bool palindrome = false;
if (cnt >= 1) {
// length of palindrome can be 2
if (dig == prv % 10) palindrome = true;
}
if (cnt >= 2) {
// length of palindrome can be 3
int nw = prv * 10 + dig; // new number
string s = "";
while(s.size() < 3) s += (char)(nw % 10 + '0'), nw /= 10;
if (s[0] == s[2]) palindrome = true;
}
if (palindrome) continue;
// ---------------------------------------------
int cur = (prv % 10) * 10 + dig;
int nok = ok;
int ncnt = cnt;
if (dig < mx) nok = 1;
if (cnt == 0 && dig != 0) ncnt = 1;
else if (cnt != 0) ++ncnt, ncnt = min(ncnt, 2);
dp[i][nok][ncnt][cur] += dp[i-1][ok][cnt][prv];
}
}
}
}
}
long long res = 0;
for (int suf = 0; suf < 100; ++suf) {
for (int cnt = 1; cnt <= 2; ++cnt)
res += dp[18][0][cnt][suf] + dp[18][1][cnt][suf];
}
return res;
}
void analyze(int x[], long long X) {
for (int i = 1; i <= 18; ++i) {
x[i] = X % 10;
X /= 10;
}
reverse(x + 1, x + 19);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> A >> B;
if (A == 0) {
if (B == 0) printf("1\n");
else analyze(b, B), printf("%lld\n", calc(b) + 1);
} else {
--A;
analyze(a, A);
analyze(b, B);
printf("%lld\n", calc(b) - calc(a));
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2268 KB |
Output is correct |
2 |
Correct |
0 ms |
2268 KB |
Output is correct |
3 |
Correct |
0 ms |
2268 KB |
Output is correct |
4 |
Correct |
0 ms |
2268 KB |
Output is correct |
5 |
Correct |
0 ms |
2268 KB |
Output is correct |
6 |
Correct |
0 ms |
2268 KB |
Output is correct |
7 |
Correct |
0 ms |
2268 KB |
Output is correct |
8 |
Correct |
0 ms |
2268 KB |
Output is correct |
9 |
Correct |
0 ms |
2268 KB |
Output is correct |
10 |
Correct |
0 ms |
2268 KB |
Output is correct |
11 |
Correct |
0 ms |
2268 KB |
Output is correct |
12 |
Correct |
0 ms |
2268 KB |
Output is correct |
13 |
Correct |
0 ms |
2268 KB |
Output is correct |
14 |
Correct |
0 ms |
2268 KB |
Output is correct |
15 |
Correct |
0 ms |
2268 KB |
Output is correct |
16 |
Correct |
0 ms |
2268 KB |
Output is correct |
17 |
Correct |
0 ms |
2268 KB |
Output is correct |
18 |
Correct |
0 ms |
2268 KB |
Output is correct |
19 |
Correct |
0 ms |
2268 KB |
Output is correct |
20 |
Correct |
0 ms |
2268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2268 KB |
Output is correct |
2 |
Correct |
0 ms |
2268 KB |
Output is correct |
3 |
Correct |
0 ms |
2268 KB |
Output is correct |
4 |
Correct |
0 ms |
2268 KB |
Output is correct |
5 |
Correct |
0 ms |
2268 KB |
Output is correct |
6 |
Correct |
0 ms |
2268 KB |
Output is correct |
7 |
Correct |
0 ms |
2268 KB |
Output is correct |
8 |
Correct |
0 ms |
2268 KB |
Output is correct |
9 |
Correct |
0 ms |
2268 KB |
Output is correct |
10 |
Correct |
0 ms |
2268 KB |
Output is correct |
11 |
Correct |
0 ms |
2268 KB |
Output is correct |
12 |
Correct |
0 ms |
2268 KB |
Output is correct |
13 |
Correct |
0 ms |
2268 KB |
Output is correct |
14 |
Correct |
0 ms |
2268 KB |
Output is correct |
15 |
Correct |
0 ms |
2268 KB |
Output is correct |
16 |
Correct |
0 ms |
2268 KB |
Output is correct |
17 |
Correct |
0 ms |
2268 KB |
Output is correct |
18 |
Correct |
0 ms |
2268 KB |
Output is correct |
19 |
Correct |
0 ms |
2268 KB |
Output is correct |
20 |
Correct |
0 ms |
2268 KB |
Output is correct |
21 |
Correct |
0 ms |
2268 KB |
Output is correct |
22 |
Correct |
0 ms |
2268 KB |
Output is correct |
23 |
Correct |
0 ms |
2268 KB |
Output is correct |
24 |
Correct |
0 ms |
2268 KB |
Output is correct |
25 |
Correct |
0 ms |
2268 KB |
Output is correct |
26 |
Correct |
0 ms |
2268 KB |
Output is correct |
27 |
Correct |
0 ms |
2268 KB |
Output is correct |
28 |
Correct |
0 ms |
2268 KB |
Output is correct |
29 |
Correct |
0 ms |
2268 KB |
Output is correct |
30 |
Correct |
0 ms |
2268 KB |
Output is correct |
31 |
Correct |
0 ms |
2268 KB |
Output is correct |
32 |
Correct |
0 ms |
2268 KB |
Output is correct |
33 |
Correct |
0 ms |
2268 KB |
Output is correct |
34 |
Correct |
0 ms |
2268 KB |
Output is correct |
35 |
Correct |
0 ms |
2268 KB |
Output is correct |
36 |
Correct |
0 ms |
2268 KB |
Output is correct |
37 |
Correct |
0 ms |
2268 KB |
Output is correct |
38 |
Correct |
0 ms |
2268 KB |
Output is correct |
39 |
Correct |
0 ms |
2268 KB |
Output is correct |
40 |
Correct |
0 ms |
2268 KB |
Output is correct |
41 |
Correct |
0 ms |
2268 KB |
Output is correct |
42 |
Correct |
0 ms |
2268 KB |
Output is correct |
43 |
Correct |
0 ms |
2268 KB |
Output is correct |
44 |
Correct |
0 ms |
2268 KB |
Output is correct |
45 |
Correct |
0 ms |
2268 KB |
Output is correct |