#include <bits/stdc++.h>
#define ii pair<int,int>
#define f first
#define s second
#define pb push_back
#define ll long long
#define endl '\n'
using namespace std;
const ll MAXN = 1e18;
ll mod = 1e9 + 7;
ll a[100005];
ll dp[20][2][11][11][2];
string s;
ll Calc(ll pos,bool check,ll digit1,ll digit2,bool start)
{
if(pos == s.size())
{
return 1;
}
ll ans = 0;
if(dp[pos][check][digit1][digit2][start] != -1)
{
return dp[pos][check][digit1][digit2][start];
}
ll maxdigit = 0;
if(check)
{
maxdigit = s[pos] - '0';
}
else
{
maxdigit = 9;
}
if(!start)
{
for(ll i = 0;i <= maxdigit;i++)
{
bool newcheck = check & (i == maxdigit);
ll newdigit1 = i;
ll newdigit2 = digit1;
if(i == 0)
{
newdigit1 = -1;
}
ll newstart = 0;
if(i != 0)
{
start = 1;
}
else
{
start = 0;
}
ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
}
}
else
{
for(ll i = 0;i <= maxdigit;i++)
{
if(i == digit1 or i == digit2)
{
continue;
}
bool newcheck = check & (i == maxdigit);
ll newdigit1 = i;
ll newdigit2 = digit1;
ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
}
}
return dp[pos][check][digit1 + 1][digit2 + 1][start] = ans;
}
ll ans(ll a)
{
if(a <= 0)
{
return 0;
}
memset(dp,-1,sizeof(dp));
s = to_string(a);
return Calc(0,0,-1,-1,0);
}
void AcSolution()
{
ll a,b;
cin >> a >> b;
cout << ans(b) - ans(a - 1) << endl;
}
signed main()
{
// freopen("BDIGIT.inp", "r",stdin);
// freopen("BDIGIT.out", "w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
{
// memset(dp,-1,sizeof(dp));
AcSolution();
}
}
Compilation message
numbers.cpp: In function 'long long int Calc(long long int, bool, long long int, long long int, bool)':
numbers.cpp:16:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(pos == s.size())
| ~~~~^~~~~~~~~~~
numbers.cpp:45:7: warning: unused variable 'newstart' [-Wunused-variable]
45 | ll newstart = 0;
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
356 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
36 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
37 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
38 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
39 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
44 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
45 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |