Submission #255580

#TimeUsernameProblemLanguageResultExecution timeMemory
255580shayan_pPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
1 ms384 KiB
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;

ll dp[11][11][20], ans;
int L[20], R[20];

void inp(int *a){
    ll x;
    cin >> x;
    for(int i = 0; i < 20; i++, x/=10)
	a[i] = x % 10;	
}

int a[20], frs = 19;

void go(int nw, int frs, bool okL, bool okR){
    if(okL && okR){
	ans+= dp[(nw + 2 <= frs) ? a[nw+2] : 10][(nw + 1 <= frs) ? a[nw + 1] : 10][nw + 1];
	return;
    }
    if(nw == -1){
	ans++;
	return;
    }
    for(int i = 0; i < 10; i++){
	if(okL == 0 && i < L[nw])
	    continue;
	if(okR == 0 && R[nw] < i)
	    continue;
	if(nw + 1 <= frs && i == a[nw+1])
	    continue;
	if(nw + 2 <= frs && i == a[nw+2])
	    continue;
	a[nw] = i;
	go(nw-1, frs == nw ? (i == 0 ? nw-1 : nw) : frs , okL || (L[nw] < i), okR || (i < R[nw])); 
    }	
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    for(int i = 0; i < 10; i++)
	for(int j = 0; j < 10; j++)
	    dp[i][j][0] = 1;
    for(int i = 0; i < 10; i++)
	dp[10][i][0] = 1;
    dp[10][10][0] = 1;
    for(int i = 1; i < 20; i++){
	for(int j = 0; j <= 10; j++){
	    for(int k = 0; k <= 10; k++){
		for(int w = 0; w < 10; w++){
		    if(k != w && j != w)
			dp[j][k][i]+= dp[k][w][i-1];
		}
	    }
	}
    }
    inp(L), inp(R); // L = zero
    go(19, 19, 0, 0);
    return cout << ans << endl, 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...