#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, b) for (int i = (a); i < (b); i++)
typedef long long ll;
const int MAX_DIGITS = 20;
ll cnt[MAX_DIGITS][10];
ll cnt_digits[MAX_DIGITS];
ll a, b;
ll dp[11][11][MAX_DIGITS][2];
vector<int> num;
ll backtrack(int prev1, int prev2, int pos, int state){
if (pos >= num.size())
return 1;
if(dp[prev1][prev2][pos][state] != -1)
return dp[prev1][prev2][pos][state];
int limit = 0;
if(state == 0){
limit = num[pos];
} else
{
limit = 9;
}
ll res = 0;
for(int i = 0; i <= limit; i++){
int nstate = state;
if (i < limit) nstate = 1;
if (i != prev1 && i != prev2)
res += backtrack(i, prev1, pos + 1, nstate);
}
dp[prev1][prev2][pos][state] = res;
return dp[prev1][prev2][pos][state];
}
ll solve(int x){
if (x < 0)
return 0;
num.clear();
while(x > 0){
num.push_back(x % 10);
x = x / 10;
}
reverse(num.begin(), num.end());
ll res = 1;
if (num.size() == 0)
return res;
memset(dp, -1, sizeof(dp));
for(int d = 1; d <= num[0]; d++){
res += backtrack(d, d, 1, d != num[0]);
}
for(int k = 2; k <= num.size(); k++){
for(int d = 1; d <= 9; d++){
res += backtrack(d, d, k, 1);
}
}
return res;
}
int main(){
cin >> a >> b;
if (a == 0)
cout << solve(b) << "\n";
else
cout << solve(b) - solve(a - 1);
}
Compilation message
numbers.cpp: In function 'll backtrack(int, int, int, int)':
numbers.cpp:15:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pos >= num.size())
~~~~^~~~~~~~~~~~~
numbers.cpp: In function 'll solve(int)':
numbers.cpp:52:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 2; k <= num.size(); k++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
416 KB |
Output isn't correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
27 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
31 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
32 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
33 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
34 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
35 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
36 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
37 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
38 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
39 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
40 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
41 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
44 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
45 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |