#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[10][10][20][2]; ///cifrele i, j; lungime k; am voie sau nu cifra
string s;
ll calc(int fst, int sec, int n, int da)
{
int i, j;
if (n >= s.size())
return 1;
if (dp[fst][sec][n][da] != -1)
return dp[fst][sec][n][da];
ll rez = 0;
if (da)
{
for (i = 0; i < 10; i++)
if (i != fst && i != sec)
rez += calc(sec, i, n + 1, 1);
}
else
{
for (i = 0; i < s[n] - '0'; i++)
if (i != fst && i != sec)
rez += calc(sec, i, n + 1, 1);
if (s[n] - '0' != fst && s[n] - '0' != sec)
rez += calc(sec, s[n] - '0', n + 1, 0);
}
dp[fst][sec][n][da] = rez;
return rez;
}
ll ans(ll nr)
{
int i,j;
s.clear();
if (nr < 0)
return 0;
if (nr == 0)
return 1;
memset(dp, -1, sizeof(dp));
while (nr)
{
s = s + char('0' + nr % 10);
nr /= 10;
}
reverse(s.begin(), s.end());
ll a = 0;
for (i = 1; i < s[0] - '0'; i++)
a += calc(i, i, 1, 1);
a += calc(s[0] - '0', s[0] - '0', 1, 0);
for (i = 2; i <= s.size(); i++)
for (j = 1; j <= 9; j++)
a += calc(j, j, i, 1);
return a;
}
int main()
{
ll a, b;
cin >> a >> b;
cout << ans(b) - ans(a - 1);
return 0;
}
Compilation message
numbers.cpp: In function 'long long int calc(int, int, int, int)':
numbers.cpp:10:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | if (n >= s.size())
| ~~^~~~~~~~~~~
numbers.cpp:9:12: warning: unused variable 'j' [-Wunused-variable]
9 | int i, j;
| ^
numbers.cpp: In function 'long long int ans(long long int)':
numbers.cpp:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (i = 2; i <= s.size(); i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
296 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
300 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |