Submission #1295077

#TimeUsernameProblemLanguageResultExecution timeMemory
1295077ulvixPalindrome-Free Numbers (BOI13_numbers)C++20
100 / 100
1 ms736 KiB
#include <bits/stdc++.h>
#ifdef ULVI
    #define db(x)          cerr<<"[ "<<#x<<" = "<<(x)<<" ]\n"
    #define dbv(v)         cerr<<#v<<" = [ ";for(auto &__x : v)cerr<<__x<<' ';cerr<<"]\n"
    #define line()         cerr<<string(80, '-')<<'\n'
#else
    #define db(x)
    #define dbv(v)
    #define line()
#endif
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define ss second
#define enld endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll sz=2e5+100;
const ll mod=1e9+7;
const ll inf=1e18;
template<class T>
using indexed_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll dp[50][12][12][2];
ll f(string &num,ll pos,ll last1,ll last2,ll tight){
    ll n=num.size(),res=0;
    ll &d=dp[pos][last1+1][last2+1][tight];
    if(d!=-1) return d;
    if(pos==n) return d=1;
    ll r=(tight?num[pos]-'0':9);
    for(ll i=0;i<=r;i++){
        if(i==last1 || i==last2) continue;
        if(i==0 && last1==-1 && last2==-1){
            res+=f(num,pos+1,last1,last2,tight&&(i==r));
            continue;
        }
        res+=f(num,pos+1,i,last1,tight&&(i==r));
    }
    db(res);
    line();
    return d=res;
}
void solve(){
    ll l;
    string r;
    cin>>l>>r;
    string _l=to_string(l-1);
    memset(dp,-1,sizeof(dp));
    ll a=(l==0?0:f(_l,0,-1,-1,1));
    memset(dp,-1,sizeof(dp));
    ll b=f(r,0,-1,-1,1);
    cout<<b-a<<'\n';
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t=1;
    //cin>>t;
    for(ll _=1;_<=t;_++){
        //cout<<"Scenario #"<<_<<":\n";
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...