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 fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"
#define int long long
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int dp[20][10][11];
string ts(int x){
stringstream ss; ss << x;
string res; ss >> res;
return res;
}
bool ok(string s){
for(int i=0; i+1<len(s); i++) if(s[i] == s[i+1]) return 0;
for(int i=1; i+1<len(s); i++) if(s[i+1] == s[i-1]) return 0;
return 1;
}
int calc(int x){
memset(dp, 0, sizeof dp);
string s = ts(x);
reverse(s.begin(), s.end());
for(int j=0; j<=9; j++) dp[1][j][10] = 1;
for(int i=2; i<=len(s); i++){
for(int j=0; j<10; j++){
for(int k=0; k<10; k++){
if(j == k) continue;
for(int f=0; f<=10; f++){
if(j == f) continue;
dp[i][j][k] += dp[i-1][k][f];
}
}
}
}
int res = 0;
for(int i=1; i<len(s); i++){
for(int j=1; j<=9; j++){
for(int k=0; k<=10; k++){
res += dp[i][j][k];
}
}
}
cerr << res << endl;
for(int i=len(s); i>0 ; i--){
for(int j=(i==len(s)? 1 : 0); j<s[i-1]-'0'; j++){
if(i<len(s) && j == s[i] - '0') continue;
if(i+1<len(s) && j == s[i+1] - '0') continue;
for(int k=0; k<=10; k++){
if(i<len(s) && k == s[i] -'0') continue;
res += dp[i][j][k];
}
}
}
return res + ok(s);
}
int32_t main(){
fastIO;
//file(TASK);
int l, r;
cin >> l >> r;
cout << calc(r) - calc(l-1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |