Submission #535260

#TimeUsernameProblemLanguageResultExecution timeMemory
535260__VariattoPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
1 ms340 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define ll long long
ll dp[20][2][10][10];
ll policz(ll x){
    for(int i=1; i<=19; i++)
        for(int a=0; a<=9; a++)
            for(int b=0; b<=9; b++)
                dp[i][0][a][b]=dp[i][1][a][b]=0;
    ll x1=x, i=1, a1=-1, a2=-1;
    while(x1){
        int curr=x1%10;
        if(a1!=-1&&a2==-1) a2=curr;
        if(a1==-1) a1=curr;
        x1/=10, i++;
        if(a1!=-1&&a2!=-1) break;
    }
    for(int a=0; a<=9; a++){
        for(int b=0; b<=9; b++){
            if(a==b)
                continue;
            if((a<a2)||(a==a2 && b<=a1))
                dp[2][0][a][b]=1;
            dp[2][1][a][b]=1;
        }
    }
    while(x1){
        int curr=x1%10;
        for(int a=0; a<=9; a++){
            for(int b=0; b<=9; b++){
                for(int c=0; c<=9; c++){
                    if(a==b||b==c||a==c)
                        continue;
                    dp[i][1][a][b]+=dp[i-1][1][b][c];
                    if(a==curr)
                        dp[i][0][a][b]+=dp[i-1][0][b][c];
                    if(a<curr)
                        dp[i][0][a][b]+=dp[i-1][1][b][c];
                }
            }
        }
        x1/=10, i++;
    }
    if(x<10)
        return x+1;
    ll wyn=0;
    //cout<<dp[3][0][0][1]<<"\n";
    for(int j=2; j<i-1; j++){
        for(int a=1; a<=9; a++)
            for(int b=0; b<=9; b++)
                wyn+=dp[j][1][a][b];
    }
    for(int a=1; a<=9; a++)
        for(int b=0; b<=9; b++)
            wyn+=dp[i-1][0][a][b];

    return wyn+10;
}
ll x, y;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin>>x>>y;
    cout<<policz(y)-policz(x-1)<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...