#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 1e5 + 5, mod = 1e9 + 7, inf = 1e18;
ll dp[20][12][12][2];
ll bachtrach(string &lim, int ith, int last1, int last2, bool smaller)
{
if(ith == lim.size()) return 1;
if(dp[ith][last1+1][last2+1][smaller] != -1)
{
return dp[ith][last1+1][last2+1][smaller];
}
int curdig = lim[ith]-'0';
ll ans = 0;
if(last1 == -1)
{
if(smaller)
{
for(int i = 1; i <= 9; ++i)
{
if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
}
ans += bachtrach(lim, ith + 1, -1, last1, 1);
}
else
{
for(int i = 1; i < curdig; ++i)
{
if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
}
if(curdig != 0)
{
// ans += bachtrach(lim, ith + 1, -1, last1, 1);
ans += bachtrach(lim, ith + 1, curdig, last1,0);
ans += bachtrach(lim, ith + 1, -1, last1, 1);
}
else ans += bachtrach(lim, ith + 1, -1, last1, 0);
}
}
else if(smaller)
{
for(int i = 0; i <= 9; ++i)
{
if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
}
}
else
{
for(int i = 0; i < curdig; ++i)
{
if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
}
if(curdig != last1 && curdig != last2)
ans += bachtrach(lim, ith + 1, curdig, last1, 0);
}
return dp[ith][last1+1][last2+1][smaller] = ans;
}
ll slove(ll x)
{
if(x < 0) return 0;
string lim = to_string(x);
memset(dp, -1, sizeof(dp));
return bachtrach(lim, 0, -1, -1, 0);
}
int main()
{
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
cin.tie(0)->sync_with_stdio(0);
ll a, b;
cin >> a >> b;
cout << slove(b) - slove(a - 1);
}
Compilation message
numbers.cpp: In function 'long long int bachtrach(std::string&, int, int, int, bool)':
numbers.cpp:8:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | if(ith == lim.size()) return 1;
| ~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |