#include <bits/stdc++.h>
using namespace std;
/***
Count the number of ways from 0 to N such that:
* no two digits are the same
* there is no palindrome of lenght three inside it
***/
const int MAXL = 25;
long long dp[MAXL][MAXL][MAXL][2];
bool calculated[MAXL][MAXL][MAXL][2];
vector<int> getDigits(long long n) {
vector<int> v;
do {
v.push_back(n % 10);
n /= 10;
} while (n > 0);
reverse(v.begin(), v.end());
return v;
}
long long f(int i, int befLast, int last, bool smaller, const vector<int> &v) {
if (i == (int)v.size()) {
calculated[i][befLast][last][smaller] = true;
dp[i][befLast][last][smaller] = 1;
return dp[i][befLast][last][smaller];
}
if (calculated[i][befLast][last][smaller]) {
return dp[i][befLast][last][smaller];
}
long long ans = 0;
int l = v[i];
if (smaller) {
l = 9;
}
for (int d = 0; d <= l; d++) {
if (d == last || d == befLast) {
continue;
}
bool nxtSmall = true;
if (!smaller && d == l) {
nxtSmall = false;
}
ans += f(i + 1, last, d, nxtSmall, v);
}
dp[i][befLast][last][smaller] = ans;
calculated[i][befLast][last][smaller] = true;
return ans;
}
long long solve(long long n) {
if (n <= 10) {
return 0;
}
memset(dp, 0, sizeof dp);
memset(calculated, false, sizeof calculated);
auto digits = getDigits(n);
return f(0, 11, 12, false, digits);
}
int main() {
long long a, b; cin >> a >> b;
cout << solve(b) - solve(a - 1) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
564 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
532 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
568 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |