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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ld long double
#define fi first
#define se second
#define ll long long
#define ss(x) (int) x.size()
#define mp make_pair
#define FOR(i, n) for(int i = 1; n >= i; ++i)
using namespace std;
using namespace __gnu_pbds;
const int nax = 1e5 + 5, pod = (1 << 19), mod = 998244353;
const ll inf = 1e18;
ll dp[20][10][10][2];
ll daj(string x, int ile) {
if(ile == 1)
return x[0] - '0';
for(int i = 1; i <= ile; ++i)
for(int j = 0; j <= 9; ++j)
for(int k = 0; k <= 9; ++k)
dp[i][j][k][0] = dp[i][j][k][1] = 0;
for(int i = 1; i <= 9; ++i) {
for(int j = 0; j <= 9; ++j) {
if(i == j)
continue;
int fir = x[0] - '0';
int sec = x[1] - '0';
if(fir > i || (fir == i && sec > j))
dp[2][i][j][0] = 1;
else if(fir == i && sec == j)
dp[2][i][j][1] = 1;
}
}
for(int i = 2; ile > i; ++i) {
for(int j = 0; j <= 9; ++j)
for(int k = 0; k <= 9; ++k) {
for(int nowa = 0; nowa <= 9; ++nowa) {
if(nowa != j && nowa != k)
dp[i + 1][k][nowa][0] += dp[i][j][k][0];
}
for(int nowa = 0; nowa <= x[i] - '0'; ++nowa) {
if(nowa != j && nowa != k)
dp[i + 1][k][nowa][(nowa == x[i] - '0')] += dp[i][j][k][1];
}
}
}
ll ans = 0;
for(int i = 0; i <= 9; ++i)
for(int j = 0; j <= 9; ++j)
ans += dp[ile][i][j][0] + dp[ile][i][j][1];
return ans;
}
ll f(string x) {
string y = "9";
int r = 1;
ll res = 0;
while(ss(x) > ss(y)) {
res += daj(y, r);
y.pb('9');
r++;
}
res += daj(x, r);
return res;
}
int check(string x) {
if(ss(x) == 1)
return 1;
for(int i = 1; ss(x) > i; ++i) {
if(x[i] == x[i - 1])
return 0;
if(i > 1 && x[i] == x[i - 2])
return 0;
}
return 1;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
string a, b;
cin >> a >> b;
cout << f(b) - f(a) + check(a) << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |