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 int long long
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
#define mod 1000000007
using namespace std;
string a,b;
int n;
int dp[20][10][10][2][3];
// n1 -> i - 1, n2 -> i - 2
int solve(int i, int n1, int n2, int tag, int z){
if(i == n) return 1;
if(dp[i][n1][n2][tag][z] != -1) return dp[i][n1][n2][tag][z];
dp[i][n1][n2][tag][z] = 0;
if(tag){
if(z == 0){
for(int j = 0; j <= 9; j++){
if(j == 0){
dp[i][n1][n2][tag][z] += solve(i + 1, n2, n1, tag, 0);
continue;
}
dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 1);
}
}
else if(z == 1){
for(int j = 0; j <= 9; j++){
if(j != n1) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 2);
}
}else{
for(int j = 0; j <= 9; j++){
if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 2);
}
}
}else{
if(z == 0){
int x = b[i] - '0';
dp[i][n1][n2][tag][z] += solve(i + 1, n1, n2, 1, 0);
for(int j = 1; j < x; j++){
dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 1);
}
dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 1);
}
else if(z == 1){
int x = b[i] - '0';
for(int j = 0; j < x; j++){
if(j != n1) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 2);
}
if(x != n1) dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 2);
}else{
int x = b[i] - '0';
for(int j = 0; j < x; j++){
//cout << n2 << ' ' << n1 << ' ' << j << endl;
if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 2);
}
if(x != n1 && x != n2) dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 2);
}
}
return dp[i][n1][n2][tag][z];
}
int solve2(int i, int n1, int n2, int tag, int z){
if(i == n) return 1;
if(dp[i][n1][n2][tag][z] != -1) return dp[i][n1][n2][tag][z];
dp[i][n1][n2][tag][z] = 0;
if(tag){
if(z == 0){
for(int j = 0; j <= 9; j++){
if(j == 0){
dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 0);
continue;
}
dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 1);
}
}
else if(z == 1){
for(int j = 0; j <= 9; j++){
if(j != n1) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 2);
}
}else{
for(int j = 0; j <= 9; j++){
if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 2);
}
}
}else{
if(z == 0){
int x = a[i] - '0';
dp[i][n1][n2][tag][z] += solve2(i + 1, n1, n2, 1, 0);
for(int j = 1; j < x; j++){
dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 1);
}
dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 1);
}
else if(z == 1){
int x = a[i] - '0';
for(int j = 0; j < x; j++){
if(j != n1) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 2);
}
if(x != n1) dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 2);
}else{
int x = a[i] - '0';
for(int j = 0; j < x; j++){
if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 2);
}
if(x != n1 && x != n2) dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 2);
}
}
return dp[i][n1][n2][tag][z];
}
signed main(){
boost;
cin >> a >> b;
n = b.size();
for(int i = 0; i < 20; i++){
for(int j = 0; j <= 9; j++){
for(int k = 0; k <= 9; k++){
for(int l = 0; l < 2; l++){
for(int z = 0; z < 3; z++){
dp[i][j][k][l][z] = -1;
}
}
}
}
}
int ans = solve(0,0,0,0,0);
n = a.size();
for(int i = 0; i < 20; i++){
for(int j = 0; j <= 9; j++){
for(int k = 0; k <= 9; k++){
for(int l = 0; l < 2; l++){
for(int z = 0; z < 3; z++){
dp[i][j][k][l][z] = -1;
}
}
}
}
}
ans -= solve2(0,0,0,0,0);
int good = 1;
for(int i = 0; i < n; i++){
if(i - 1 >= 0){
if(a[i] == a[i - 1]) good = 0;
}
if(i - 2 >= 0){
if(a[i] == a[i - 2]) good = 0;
}
}
if(a == "0" && b == "0"){
cout << 1;
return 0;
}
if(a == "0") good++;
cout << ans + good;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |