71882024-01-02 15:10:11anonSzivárványszámokcpp17Accepted 45/454ms4332 KiB
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
using namespace std;
ll solve(string N, ll limit, bool left, unordered_map<string, ll> &dp) {
    ll i, ans;
    string key = N + " " + to_string(limit) + " " + (left ? "1" : "0");
    auto found = dp.find(key);
    if(found == dp.end()) {
        if(N.empty())
            ans = !left;
        else {
            ans = 0;
            if(left) {
                for(i = limit; i <= (N[0] - '0'); i++) {
                    string ns;
                    if(i == (N[0] - '0'))
                        ns = N.substr(1);
                    else
                        ns = string(N.size() - 1, '9');
                    ans += solve(ns, i, true, dp) + solve(ns, i - 1, false, dp);
                }
            }
            else {
                for(i = min(limit, (ll) (N[0] - '0')); i >= 0; i--) {
                    string ns;
                    if(i == (N[0] - '0'))
                        ns = N.substr(1);
                    else
                        ns = string(N.size() - 1, '9');
                    ans += solve(ns, i, false, dp);
                }
            }
        }
        dp[key] = ans;
    }
    else
        ans = found->second;
    return ans;
}
int main() {
    FastIO;
    ll i, ans;
    string N;
    unordered_map<string, ll> dp;
    cin >> N;
    if(N == "0") {
        cout << "0\n";
        return 0;
    }
    for(i = N.size() - 1; i >= 0; i--) {
        N[i]--;
        if(N[i] == '0' - 1)
            N[i] = '9';
        else
            break;
    }
    ans = solve(N, 1, true, dp);
    for(i = 0; i < N.size(); i++)
        ans += solve(string(i, '9'), 1, true, dp);
    cout << ans + 1 << '\n';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/03ms1836 KiB
2Accepted0/03ms2064 KiB
3Accepted0/04ms2536 KiB
4Accepted1/13ms2460 KiB
5Accepted1/13ms2664 KiB
6Accepted1/13ms2872 KiB
7Accepted1/13ms3092 KiB
8Accepted1/13ms3168 KiB
9Accepted1/13ms3268 KiB
10Accepted1/13ms3272 KiB
11Accepted1/13ms3300 KiB
12Accepted1/13ms3516 KiB
13Accepted2/23ms3632 KiB
14Accepted2/23ms3628 KiB
15Accepted2/23ms3632 KiB
16Accepted2/23ms3708 KiB
17Accepted1/13ms3704 KiB
18Accepted2/24ms3856 KiB
19Accepted2/24ms3872 KiB
20Accepted2/24ms3972 KiB
21Accepted3/34ms4060 KiB
22Accepted2/23ms3844 KiB
23Accepted2/23ms4044 KiB
24Accepted2/23ms4256 KiB
25Accepted2/24ms4332 KiB
26Accepted2/24ms4332 KiB
27Accepted2/24ms4272 KiB
28Accepted2/24ms4308 KiB
29Accepted2/24ms4300 KiB
30Accepted2/24ms4296 KiB