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 lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
ll dp[20][10][10];
ll sum[20][10];
inline void prec() {
for(int a = 0; a < 10; a++) {
for(int b = 0; b < 10; b++) {
if(a != b) {
dp[2][b][a] = 1;
}
}
}
for(int i = 3; i < 20; i++) {
for(int a = 0; a < 10; a++) {
for(int b = 0; b < 10; b++) {
for(int c = 0; c < 10; c++) {
if(a != b && a != c) {
dp[i][b][a] += dp[i - 1][c][b];
}
}
}
}
}
for(int i = 0; i < 10; i++) {
sum[1][i] = 1;
}
for(int i = 2; i < 20; i++) {
for(int a = 0; a < 10; a++) {
for(int b = 0; b < 10; b++) {
sum[i][a] += dp[i][b][a];
}
}
}
}
inline ll solve(ll x) {
if(x == 0) {
return -1;
}
vector <int> dig;
while(x > 0) {
dig.push_back(x % 10);
x /= 10;
}
int sz = dig.size();
ll ans = 0;
for(int i = sz - 1; i > 0; i--) {
for(int a = 1; a < 10; a++) {
ans += sum[i][a];
}
}
int c1 = -1, c2 = -2, c3 = -3;
for(int i = sz - 1; i >= 0; i--) {
for(int a = (i == sz - 1); a < dig[i]; a++) {
if(i == 0) {
if(a != c2 && a != c3) {
ans++;
}
}
else {
for(int b = 0; b < 10; b++) {
if(a != c2 && a != c3 && b != a && b != c3) {
ans += dp[i + 1][b][a];
//cerr << b << " " << a << "\n";
}
}
}
}
//cerr << "\n";
//cerr << ans << " ";
c1 = c2;
c2 = c3;
c3 = dig[i];
if(c1 == c2 || c1 == c3 || c2 == c3) {
break;
}
}
//cerr << "\n";
//cerr << ans << "\n";
return ans;
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
//int ;
ll a, b;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> a >> b;
prec();
cout << solve(b + 1) - solve(a);
//cin.close();
//cout.close();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |