#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[MAX_DIGITS][11][11][2];
vector<int> num;
int curr_num[MAX_DIGITS];
ll backtrack(int pos, int prev1, int prev2, int state)
{
if (pos == num.size())
return 1;
if (dp[pos][prev1][prev2][state] != -1)
{
return dp[pos][prev1][prev2][state];
}
int limit;
if (state == 0)
limit = num[pos];
else
{
limit = 9;
}
ll res = 0;
int start;
if (pos == 0)
{
start = 1;
} else
{
start = 0;
}
for (int i = start; i <= limit; i++)
{
int nstate = state;
if (i < limit)
nstate = 1;
if (i != prev1 && i != prev2)
res += backtrack(pos + 1, i, prev1, nstate);
}
dp[pos][prev1][prev2][state] = res;
return dp[pos][prev1][prev2][state];
}
ll solve(ll x){
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;
loop(i, 1, num.size()) res += cnt_digits[i];
memset(dp, -1, sizeof(dp));
res += backtrack(0, 10, 10, 0);
return res;
}
int main()
{
cin >> a >> b;
loop(i, 1, 10)
{
cnt[1][i] = 1;
}
loop(pos, 2, MAX_DIGITS)
{
loop(i, 0, 10)
{
loop(d, 0, 10)
{
if (d != i)
cnt[pos][i] += cnt[pos - 1][d] - cnt[pos - 2][i];
}
}
}
loop(i, 1, MAX_DIGITS)
{
loop(j, 0, 10)
cnt_digits[i] += cnt[i][j];
}
// cout << cnt_digits[4] << "\n";
ll ans;
if (a == 0){
if ( b == 0)
ans = 0;
else
ans = solve(b);
} else
ans = solve(b) - solve(a - 1);
cout << ans << "\n";
}
Compilation message
numbers.cpp: In function 'll backtrack(int, int, int, int)':
numbers.cpp:17:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pos == num.size())
~~~~^~~~~~~~~~~~~
numbers.cpp: In function 'll solve(ll)':
numbers.cpp:4:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define loop(i, a, b) for (int i = (a); i < (b); i++)
^
numbers.cpp:61:5: note: in expansion of macro 'loop'
loop(i, 1, num.size()) res += cnt_digits[i];
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
14 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
376 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |