This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
123 321
*/
#include <bits/stdc++.h>
using namespace std;
int64_t f[20][10][10][2];
vector<int> convert(int64_t x) {
vector<int> v;
if(x == 0) {v.push_back(x); return v;}
while(x > 0) v.push_back(x % 10), x /= 10;
reverse(v.begin(), v.end()); return move(v);
}
int64_t cal(vector<int> x) {
memset(f, 0, sizeof f);
int n = (int)x.size();
if(n == 1) return x[0];
for(int i = 10; i < 100; ++ i)
for(int j = 0; j + 1 < n; ++ j)
if((i / 10) != (i % 10) && (j || (i / 10 <= x[j]))) {
if((j == 0) && (i / 10 == x[j]) && (i % 10 > x[j + 1])) continue;
f[j + 1][i / 10][i % 10][j || (i / 10 < x[j]) || (i % 10 < x[j + 1])] = 1;
}
for(int i = 1; i < n - 1; ++ i)
for(int num1 = 0; num1 <= 9; ++ num1)
for(int num2 = 0; num2 <= 9; ++ num2)
for(int nxt = 0; nxt <= 9; ++ nxt) if(num1 != nxt && num2 != nxt) {
f[i + 1][num2][nxt][1] += f[i][num1][num2][1];
if(nxt == x[i + 1]) f[i + 1][num2][nxt][0] += f[i][num1][num2][0];
if(nxt < x[i + 1]) f[i + 1][num2][nxt][1] += f[i][num1][num2][0];
}
int64_t Ans = 0;
for(int i = 0; i <= 9; ++ i)
for(int j = 0; j <= 9; ++ j) Ans += f[n - 1][i][j][1];
return Ans + 10;
}
int main(){
int64_t a, b; cin >> a >> b;
// cout << cal(convert(b + 1)) << ' ' << cal(convert(a)) << endl;
cout << cal(convert(b + 1)) - cal(convert(a)) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |