Submission #338884

#TimeUsernameProblemLanguageResultExecution timeMemory
338884limabeansPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
2 ms384 KiB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;










ll dp[20][2][11][11][2];

void reset() {
    memset(dp,-1,sizeof(dp));
}



ll solve(string s, int i, bool lower, int l0, int l1, bool started) {
    int n = s.length();
    if (i == n) {
	return started;
    } else {
	ll &res = dp[i][lower][l0][l1][started];
	if (~res) return res;
	res = 0;

	//cout<<i<<": "<<lower<<" "<<l0<<" "<<l1<<" "<<started<<endl;
	
	
	for (int d=0; d<=(lower?9:int(s[i]-'0')); d++) {
	    if (d==l0) continue;
	    if (d==l1) continue;
	    
	    bool nlower = lower;
	    if (d<int(s[i]-'0')) nlower = true;
	    
	    bool nstarted = started;
	    if (d>0) nstarted = true;
	    
	    if (!nstarted) nlower = true;

	    int nl0 = l0;
	    int nl1 = l1;

	    if (nstarted) {
		nl0 = l1;
		nl1 = d;
	    }
	    
	    res += solve(s, i+1, nlower, nl0, nl1, nstarted);
	}

	return res;
    }
}



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


    ll a,b;
    cin>>a>>b;
    // [a,b]


    reset();
    ll res = solve(to_string(b),0,false,10,10,false);

    
    reset();
    if (a>0) {
	res -= solve(to_string(a-1),0,false,10,10,false);
    } else {
	res++; //add 0
    }

    out(res);    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...