// teddybear's code
// the one who loves NBP
// noe the second
// goal: 0 / 8
// get medal in APIO (like TKN)
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
// prob:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define FOR(i,n) for (int i = 0; i<n; i++)
using ll = long long;
using ull = unsigned long long;
ll n,m,t;
ll cnt = 0;
const int maxn = 1e5;
const ll mod = 1e9 + 7;
#define Y "YES"
#define N "NO"
int a[1005][1005];
bool visited[1005][1005];
int dist[1005][1005];
int w,h;
int fx[4] = {-1, 1, 0, 0};
int fy[4] = {0, 0, -1, 1};
queue <pair<int,int>> q;
string s;
ll num[20][2][12][12];
ll dp(ll pos, ll eq, ll last1, ll last2, ll start)
{
if (pos == s.size())
{
return 1;
}
ll ans = 0;
if (num[pos][eq][last1][last2] != -1)
{
return num[pos][eq][last1][last2];
}
ll mxnum;
if (eq)
{
mxnum = s[pos] - '0';
}
else
{
mxnum = 9;
}
if (start == -1)
{
for (ll dig = 0; dig<=mxnum; dig++)
{
if ((dig == last1 && last1 > 0) || (dig == last2 && last2 > 0))
{
continue;
}
ll neweq = eq & (dig == mxnum);
ll newlast1 = dig;
ll newlast2 = last1;
if (dig != 0)
{
start = 0;
}
else
{
start = -1;
}
ans += dp(pos+1,neweq,newlast1,newlast2,start);
}
}
else
{
for (ll dig = 0; dig<=mxnum; dig++)
{
if (dig == last1 || dig == last2)
{
continue;
}
ll neweq = eq & (dig == mxnum);
ll newlast1 = dig;
ll newlast2 = last1;
ans += dp(pos+1,neweq,newlast1,newlast2,start);
}
}
return num[pos][eq][last1][last2] = ans;
}
ll DP_digit(ll x)
{
if (x < 0) return 0;
s = to_string(x);
memset(num,-1,sizeof(num));
return dp(0,1,-1,-1,-1);
}
void solve()
{
ll a,b;
cin >> a >> b;
//cout << "! " << DP_digit(b) << ' ' << DP_digit(a-1) << '\n';
cout << DP_digit(b) - DP_digit(a-1) << '\n';
}
void init()
{
int te = 1; //cin >> te;
while (te--)
{
solve();
}
}
void preprocess()
{
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cin.tie(0); cout.tie(0);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
init();
preprocess();
//solve();
return 0;
}
Compilation message
numbers.cpp: In function 'll dp(ll, ll, ll, ll, ll)':
numbers.cpp:39:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if (pos == s.size())
| ~~~~^~~~~~~~~~~
numbers.cpp: In function 'll _Z8DP_digitx.part.0(ll)':
numbers.cpp:99:34: warning: array subscript -1 is below array bounds of 'll [12]' {aka 'long long int [12]'} [-Warray-bounds]
99 | return num[pos][eq][last1][last2] = ans;
| ~~~~~~~~~~~~~~~~~~~~~~~~~^
# |
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 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
288 KB |
Output isn't 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
14 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |