Submission #621318

#TimeUsernameProblemLanguageResultExecution timeMemory
621318353ceregaPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
1 ms212 KiB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>


using namespace std;


using ll = long long;
using ld = long double;

#define X first
#define Y second

//const ll mod = 1000000007;
const ll mod = 998244353;



ll calc(ll n)
{
    if (n<10) return n+1;
    string s = to_string(n);
    ll A = 1;
    ll N = s.length();
    vector<ll> p8(N);
    p8[0] = 1;
    for (ll i=1;i<N;i++) p8[i] = p8[i-1]*8;
    {
        ll x = 1;
        for (ll j=0;j+1<N;j++)
        {
            if (j<2) x *= 9;
            else x *= 8;
            A += x;
        }
    }
    p8[N-1] = 9*p8[N-2];
    for (ll i=0;i<N;i++)
    {
        for (ll d=0;d<s[i]-'0';d++)
        {
            if (d==0 and i==0) continue;
            if (i>0 and d==s[i-1]-'0') continue;
            if (i>1 and d==s[i-2]-'0') continue;
            A += p8[N-1-i];
        }
        if (i==N-1)
        {
            ll d = s[i]-'0';
            if (d==0 and i==0) continue;
            if (i>0 and d==s[i-1]-'0') continue;
            if (i>1 and d==s[i-2]-'0') continue;
            A += p8[N-1-i];
        }
        if (i>0 and s[i]==s[i-1]) break;
        if (i>1 and s[i]==s[i-2]) break;
    }
    return A;
}

void solve()
{
    ll L, R;
    cin >> L >> R;
    cout << calc(R)-calc(L-1) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    ll T;
    T = 1;
    //cin >> T;
    while (T--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...