Submission #1295280

#TimeUsernameProblemLanguageResultExecution timeMemory
1295280darkdevilvaqifPalindrome-Free Numbers (BOI13_numbers)C++20
100 / 100
2 ms724 KiB
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define all(v, l) v.begin() + l, v.end()
#define rall(v, l) v.rbegin(), v.rend() - l
#define pb push_back
#define rsz resize
#define fi first
#define se second
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
#define IMAX INT_MAX
#define IMIN INT_MIN
#define endl "\n"
#define newline cout << endl;
using namespace std;
 
// structs

// globals

// variables
ll l, r;
string A, B;

// iterators
int i;

// notes
/*
 -stuff you should look for-
* int overflow, array bounds
* special cases (n=1?)
* do something instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH

continue - skip the rest in the loop
*/

// functions
ll GCD(ll numeroune, ll numerodeux);
ll LCM(ll numeroune, ll numerodeux);
ll power(ll numeroune, ll numerodeux);
 
void solve()
{
    cin >> l >> r;
    A = '*' + to_string(l - 1), B = '*' + to_string(r);
    
    ll dp[21][11][11][2];
    function<ll(string, int, int, int, bool)> f = [&](string s, int k, int lst1, int lst2, bool tight)
    {
        if (k == (int)s.size())
        {
            return 1LL;
        }
        if (dp[k][lst1][lst2][tight] != -1)
        {
            return dp[k][lst1][lst2][tight];
        }
        
        ll sum = 0LL;
        for (char c = '0'; c <= (tight ? s[k] : '9'); c++)
        {
            if (lst1 == 10 && lst2 == 10 && c == '0')
            {
                auto temp = (tight && s[k] == '0' ? tight : false);
                sum += f(s, k + 1, lst1, lst2, temp);
                continue;
            }
            if ((c - '0') == lst1 || (c - '0') == lst2)
            {
                continue;
            }
            
            sum += f(s, k + 1, (c - '0'), lst1, (tight && s[k] == c));
        }
        
        return dp[k][lst1][lst2][tight] = sum;
    };
    
    memset(dp, -1LL, sizeof(dp));
    ll temp1 = f(B, 1, 10, 10, 1);
    memset(dp, -1LL, sizeof(dp));
    ll temp2 = (!l ? 0 : f(A, 1, 10, 10, 1));
    
    cout << temp1 - temp2;
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        newline
    }
}
 
ll GCD(ll numeroune, ll numerodeux)
{
    if (!numeroune)
    {
        return numerodeux;
    }
    return GCD(numerodeux % numeroune, numeroune);
}

ll LCM(ll numeroune, ll numerodeux)
{
    return numeroune * numerodeux / GCD(numeroune, numerodeux);
}

ll power(ll numeroune, ll numerodeux)
{
    ll res = 1;
    while (numerodeux) 
    {
        if (numerodeux & 1)
        { 
            res *= numeroune;
        }
        numeroune *= numeroune;
        numerodeux >>= 1;    
    }
    return res;
}

/*
$$$$$$$$\ $$$$$$$$\ 
$$  _____|\____$$  |
$$ |          $$  / 
$$$$$\       $$  /  
$$  __|     $$  /   
$$ |       $$  /    
$$$$$$$$\ $$$$$$$$\ 
\________|\________|
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...