#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
long a,b;
long dp[25][10][10][2][2][25];
vector<int> num;
long call(int pos, int dig1, int dig2, int notpal, int f, int nz){
if (pos == num.size()){
if (notpal) return 1;
return 0;
}
if (dp[pos][dig1][dig2][notpal][f][nz] != -1)
return dp[pos][dig1][dig2][notpal][f][nz];
long res = 0;
int lmt;
if (f == 0){
lmt = num[pos];
}
else lmt = 9;
for (int dgt=0;dgt<=lmt;dgt++){
int ndig1, ndig2, nnotpal=notpal, nf=f, nnz=nz;
if (f == 0 && dgt < lmt ) nf=1;
if (nz<=pos) nnz = nz;
else{
if (dgt != 0) nnz = pos+1;
else nnz = 23;
}
if ((pos>=3 && dgt==dig2 && pos>=nz)||
(pos>=4 && dgt==dig1 && pos>=nz+1)) nnotpal=0;
ndig1 = dig2;
ndig2 = dgt;
if (nnotpal) res+= call(pos+1, ndig1, ndig2, nnotpal, nf, nnz);
}
return dp[pos][dig1][dig2][notpal][f][nz]= res;
}
long solve(int a){
num.clear();
while (a > 0){
num.push_back(a%10);
a/=10;
}
num.push_back(0);
num.push_back(0);
reverse(num.begin(), num.end());
memset(dp, -1, sizeof(dp));
return call(2, 0, 0, 1, 0, 23);
}
int main()
{
cin >> a >> b;
if (a == 0) cout<<solve(b)<<endl;
else cout << solve(b) - solve(a-1);
return 0;
}
Compilation message
numbers.cpp: In function 'long int call(int, int, int, int, int, int)':
numbers.cpp:13:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | if (pos == num.size()){
| ~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
2124 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
4 |
Correct |
1 ms |
2124 KB |
Output is correct |
5 |
Correct |
1 ms |
2124 KB |
Output is correct |
6 |
Correct |
1 ms |
2124 KB |
Output is correct |
7 |
Correct |
1 ms |
2124 KB |
Output is correct |
8 |
Correct |
1 ms |
2288 KB |
Output is correct |
9 |
Correct |
1 ms |
2124 KB |
Output is correct |
10 |
Correct |
1 ms |
2252 KB |
Output is correct |
11 |
Correct |
1 ms |
2124 KB |
Output is correct |
12 |
Correct |
1 ms |
2124 KB |
Output is correct |
13 |
Correct |
1 ms |
2124 KB |
Output is correct |
14 |
Correct |
1 ms |
2124 KB |
Output is correct |
15 |
Correct |
1 ms |
2124 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
1 ms |
2124 KB |
Output is correct |
18 |
Correct |
1 ms |
2124 KB |
Output is correct |
19 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
20 |
Correct |
1 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2124 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2252 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
5 |
Correct |
1 ms |
2124 KB |
Output is correct |
6 |
Correct |
2 ms |
2124 KB |
Output is correct |
7 |
Correct |
2 ms |
2124 KB |
Output is correct |
8 |
Correct |
2 ms |
2124 KB |
Output is correct |
9 |
Correct |
1 ms |
2124 KB |
Output is correct |
10 |
Correct |
1 ms |
2124 KB |
Output is correct |
11 |
Correct |
2 ms |
2124 KB |
Output is correct |
12 |
Correct |
1 ms |
2124 KB |
Output is correct |
13 |
Correct |
2 ms |
2252 KB |
Output is correct |
14 |
Correct |
1 ms |
2124 KB |
Output is correct |
15 |
Correct |
2 ms |
2124 KB |
Output is correct |
16 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
18 |
Correct |
1 ms |
2124 KB |
Output is correct |
19 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
22 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
24 |
Correct |
1 ms |
2124 KB |
Output is correct |
25 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
29 |
Correct |
1 ms |
2124 KB |
Output is correct |
30 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
33 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
34 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
35 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
36 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
37 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
38 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
39 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
40 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
41 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
42 |
Correct |
1 ms |
2124 KB |
Output is correct |
43 |
Incorrect |
1 ms |
2124 KB |
Output isn't correct |
44 |
Incorrect |
2 ms |
2124 KB |
Output isn't correct |
45 |
Incorrect |
3 ms |
2252 KB |
Output isn't correct |