제출 #1355043

#제출 시각아이디문제언어결과실행 시간메모리
1355043otariusPalindrome-Free Numbers (BOI13_numbers)C++20
100 / 100
1 ms348 KiB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngl(chrono::steady_clock::now().time_since_epoch().count());

// #define int long long
// #define int unsigned long long

#define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 2 * 1e5 + 15;

ll dp[20][11][11][2];
ll rec(string &s, int pos, int d1, int d2, bool f) {
    if (pos == s.size()) return 1;
    if (dp[pos][d1 + 1][d2 + 1][f] != -1)
        return dp[pos][d1 + 1][d2 + 1][f];

    ll ans = 0;
    int mx = (f ? s[pos] - '0' : 9);
    for (int d = 0; d <= mx; d++) {
        bool f1 = (f && d == mx);
        if (d1 == -1 && d == 0) ans += rec(s, pos + 1, -1, -1, f1);
        else if (d != d1 && d != d2) ans += rec(s, pos + 1, d, d1, f1);
    }

    return dp[pos][d1 + 1][d2 + 1][f] = ans;
}
ll get(ll n) {
    string s = to_string(n);
    memset(dp, -1, sizeof(dp));
    return rec(s, 0, -1, -1, 1);
}
void solve() {
    ll a, b;
    cin >> a >> b;
    cout << get(b) - get(a - 1);
}
int32_t main() { 
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        // cout << '\n';
    }
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…