#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define sc second
#define pb push_back
#define point pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
#define show(x) cerr << (#x) << " = " << x << '\n'
#define print(x) if (1) {cerr << (#x) << " = "; \
for (auto it : x) cerr << it << ' '; cerr << '\n';}
using namespace std;
const int MAXN = 1000069;
const int MAXT = 1e7 + 69;
const int RT = 420;
const int LOG = 31;
const int MOD = 998244353;
const int INF = 2e9 + 69;
const ll MAX = 2e18 + 69;
const ld EPS = 1e-12;
#define int ll
ll dp[20][11][11][11][2];
bool check(ll n, int addlen = 0) {
string s = to_string(n);
while (s.size() < addlen)
s = "0" + s;
for (int i = 1; i < s.size(); ++i)
if (s[i] == s[i - 1])
return false;
for (int i = 2; i < s.size(); ++i)
if (s[i] == s[i - 2])
return false;
return true;
}
ll f(ll n) {
if (n <= 1e6) {
ll ans = 0;
for (int i = 0; i <= n; ++i)
ans += check(i);
return ans;
}
for (int i = 0; i < 20; ++i)
for (int j = 0; j < 11; ++j)
for (int k = 0; k < 11; ++k)
for (int l = 0; l < 11; ++l)
dp[i][j][k][l][0] = dp[i][j][k][l][1] = 0;
string s = to_string(n);
reverse(all(s)), s = "0" + s;
for (auto& c : s)
c -= '0';
for (int i = 0; i < 1000; ++i) {
if (check(i, 3)) {
dp[3][i / 100][i / 10 % 10][i % 10][0] = 1;
if (i <= n % 1000)
dp[3][i / 100][i / 10 % 10][i % 10][1] = 1;
}
}
for (int i = 4; i < s.size(); ++i) {
for (int j = 0; j < 10; ++j)
for (int k = 0; k < 10; ++k)
for (int l = 0; l < 10; ++l)
for (int p = 0; p < 10; ++p) {
int x = 1000 * s[i] + 100 * s[i - 1] + 10 * s[i - 2] + s[i - 3];
int y = 1000 * j + 100 * k + 10 * l + p;
if (check(y, 4)) {
dp[i][j][k][l][0] += dp[i - 1][k][l][p][0];
if (x >= y) {
if (j < s[i])
dp[i][j][k][l][1] += dp[i - 1][k][l][p][0];
else
dp[i][j][k][l][1] += dp[i - 1][k][l][p][1];
}
}
}
}
ll ans = 0;
for (int i = 0; i <= min(99ll, n); ++i)
ans += check(i);
for (int j = 1; j < 10; ++j)
for (int k = 0; k < 10; ++k)
for (int l = 0; l < 10; ++l)
ans += dp[s.size() - 1][j][k][l][1];
return ans;
}
signed main() {
fast_io;
ll a, b;
cin >> a >> b;
cout << f(b) - f(a - 1);
}
Compilation message
numbers.cpp: In function 'bool check(long long int, long long int)':
numbers.cpp:33:18: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
33 | while (s.size() < addlen)
| ~~~~~~~~~^~~~~~~~
numbers.cpp:36:20: 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]
36 | for (int i = 1; i < s.size(); ++i)
| ~~^~~~~~~~~~
numbers.cpp:40:20: 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]
40 | for (int i = 2; i < s.size(); ++i)
| ~~^~~~~~~~~~
numbers.cpp: In function 'long long int f(long long int)':
numbers.cpp:75:20: 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]
75 | for (int i = 4; i < s.size(); ++i) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
44 ms |
300 KB |
Output is correct |
17 |
Correct |
33 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
9 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
9 ms |
736 KB |
Output is correct |
4 |
Correct |
9 ms |
732 KB |
Output is correct |
5 |
Incorrect |
4 ms |
724 KB |
Output isn't correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Incorrect |
3 ms |
732 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
736 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
15 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
16 |
Correct |
8 ms |
724 KB |
Output is correct |
17 |
Correct |
8 ms |
724 KB |
Output is correct |
18 |
Correct |
9 ms |
724 KB |
Output is correct |
19 |
Correct |
8 ms |
724 KB |
Output is correct |
20 |
Correct |
9 ms |
724 KB |
Output is correct |
21 |
Correct |
8 ms |
724 KB |
Output is correct |
22 |
Correct |
8 ms |
724 KB |
Output is correct |
23 |
Correct |
8 ms |
724 KB |
Output is correct |
24 |
Correct |
8 ms |
724 KB |
Output is correct |
25 |
Correct |
8 ms |
724 KB |
Output is correct |
26 |
Correct |
8 ms |
724 KB |
Output is correct |
27 |
Correct |
8 ms |
724 KB |
Output is correct |
28 |
Correct |
8 ms |
724 KB |
Output is correct |
29 |
Correct |
10 ms |
724 KB |
Output is correct |
30 |
Correct |
10 ms |
724 KB |
Output is correct |
31 |
Correct |
9 ms |
724 KB |
Output is correct |
32 |
Correct |
9 ms |
732 KB |
Output is correct |
33 |
Correct |
8 ms |
724 KB |
Output is correct |
34 |
Correct |
9 ms |
724 KB |
Output is correct |
35 |
Correct |
9 ms |
724 KB |
Output is correct |
36 |
Correct |
8 ms |
732 KB |
Output is correct |
37 |
Correct |
8 ms |
736 KB |
Output is correct |
38 |
Correct |
8 ms |
724 KB |
Output is correct |
39 |
Correct |
8 ms |
732 KB |
Output is correct |
40 |
Correct |
8 ms |
724 KB |
Output is correct |
41 |
Correct |
9 ms |
732 KB |
Output is correct |
42 |
Correct |
9 ms |
736 KB |
Output is correct |
43 |
Correct |
9 ms |
740 KB |
Output is correct |
44 |
Correct |
9 ms |
724 KB |
Output is correct |
45 |
Correct |
8 ms |
724 KB |
Output is correct |