This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |