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>
using namespace std;
const int N = 20;
int n;
long long dp[N][N][N][2][2];
string s;
long long calc(int pos, int c1, int c2, int f, int z) {
if(pos == n) return 1;
auto &res = dp[pos][c1][c2][f][z];
if(res != -1) {
return res;
}
res = 0;
int lmt = f ? 9 : s[pos] - '0';
for(int i = 0; i <= lmt; i++) {
if(i == c1 || i == c2) continue;
if(z + i == 0) {
res += calc(pos + 1, 10, 10, 1, 0);
} else {
res += calc(pos + 1, c2, i, f | (i < lmt), 1);
}
}
return res;
}
long long solve(string &x) {
s = x;
n = (int) x.size();
memset(dp, -1, sizeof dp);
return calc(0, 10, 10, 0, 0);
}
bool check(long long x) {
string s = to_string(x);
int m = (int) s.size();
for(int i = 0; i < m - 1; i++) {
if(s[i] == s[i + 1]) {
return 0;
}
}
for(int i = 0; i < m - 2; i++) {
if(s[i] == s[i + 2]) {
return 0;
}
}
return 1;
}
long long brute(long long l, long long r) {
int ans = 0;
for(long long i = l + 1; i <= r; i++) {
ans += check(i);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long l, r;
cin >> l >> r;
l--;
string a, b;
a = to_string(l);
b = to_string(r);
cout << solve(b) - (l >= 0 ? solve(a) : 0) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |