#include <bits/stdc++.h>
using namespace std;
long long a, b, dp[20][11][11][3][3];
vector<long long> number;
long long derp(long long idx, long long prev1, long long prev2, long long same, long long allzero){
if(idx == number.size()) dp[idx][prev1][prev2][same][allzero] = 1;
if(dp[idx][prev1][prev2][same][allzero] != -1) return dp[idx][prev1][prev2][same][allzero];
long long sum = 0, limit;
if((!allzero && same) || (allzero && idx == 0)) limit = number[idx];
else limit = 9;
for(int i = 0; i <= limit; i++){
if(allzero){
if(i == 0) sum += derp(idx+1, 10, 10, 1, 1);
else sum += derp(idx+1, i, 10, (idx == 0 && i == limit), 0);
} else if(i != prev1 && i != prev2){
if(same && i == number[idx]) sum += derp(idx+1, i, prev1, 1, 0);
else sum += derp(idx+1, i, prev1, 0, 0);
}
}
return dp[idx][prev1][prev2][same][allzero] = sum;
}
long long solve(long long num){
if(num < 0) return 0;
number.clear();
while(num > 0){ number.push_back(num%10); num /= 10; }
reverse(number.begin(), number.end());
memset(dp, -1, sizeof(dp));
return derp(0, 10, 10, 1, 1);
}
int main(){
cin >> a >> b;
cout << solve(b)-solve(a-1);
}
Compilation message
numbers.cpp: In function 'long long int derp(long long int, long long int, long long int, long long int, long long int)':
numbers.cpp:8:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx == number.size()) dp[idx][prev1][prev2][same][allzero] = 1;
~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
552 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
480 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
504 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
2 ms |
424 KB |
Output is correct |
29 |
Correct |
3 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
504 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
2 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
504 KB |
Output is correct |
34 |
Correct |
2 ms |
504 KB |
Output is correct |
35 |
Correct |
2 ms |
504 KB |
Output is correct |
36 |
Correct |
2 ms |
504 KB |
Output is correct |
37 |
Correct |
2 ms |
504 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
2 ms |
504 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
504 KB |
Output is correct |
43 |
Correct |
2 ms |
504 KB |
Output is correct |
44 |
Correct |
2 ms |
504 KB |
Output is correct |
45 |
Correct |
2 ms |
504 KB |
Output is correct |