#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 9223372036854775807
string toString(int a){
string s = "";
if(a == 0) return "0";
while(a){
s = (char)(a%10+'0') + s;
a /= 10;
}
return s;
}
string a, b;
int memo[20][11][11][2][2][2];
int dp(int curr, int last, int lastlast, bool ok, bool isPal, bool first){
if(curr == b.size()){
if(isPal && !first) return 0;
else{
return 1;
}
}
if(memo[curr][last][lastlast][ok][isPal][first] != -1) return memo[curr][last][lastlast][ok][isPal][first];
memo[curr][last][lastlast][ok][isPal][first] = 0;
if(ok){
for(int i = 0; i <= 10; i++){
if(first && i == 1) continue;
if(!first && i == 0) continue;
if((i == last || i == lastlast) && !first){
memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,ok,1,0);
}else{
bool cond = first;
if(cond && i != 0) cond = false;
memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,ok,isPal, cond);
}
}
}else{
int u = b[curr]-'0'+1;
for(int i = 0; i <= u; i++){
if(first && i == 1) continue;
if(!first && i == 0) continue;
if((i == last || i == lastlast) && !first){
memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,i < u,1,0);
}else{
bool cond = first;
if(cond && i != 0) cond = false;
memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,i < u,isPal, cond);
}
}
}
return memo[curr][last][lastlast][ok][isPal][first];
}
void reset(){
for(int i = 0; i <= 19; i++){
for(int j = 0; j < 11; j++){
for(int k = 0; k < 11; k++){
for(int l = 0; l < 2; l++){
for(int m = 0; m < 2; m++){
for(int n = 0; n < 2; n++){
memo[i][j][k][l][m][n] = -1;
}
}
}
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int c, d;
cin >> c >> d;
a = toString(c-1);
b = toString(d);
reset();
if(c == 0){
cout << dp(0,0,0,0,0,1) << endl;
}else{
int t = dp(0,0,0,0,0,1);
b = a;
reset();
int x = dp(0,0,0,0,0,1);
t -= x;
cout << t << endl;
}
}
Compilation message
numbers.cpp: In function 'long long int dp(long long int, long long int, long long int, bool, bool, bool)':
numbers.cpp:23:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(curr == b.size()){
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
2 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
2 ms |
512 KB |
Output is correct |
44 |
Correct |
3 ms |
512 KB |
Output is correct |
45 |
Correct |
4 ms |
512 KB |
Output is correct |