52322023-04-23 15:23:49anonHázszámokcpp17Accepted 100/1003ms5300 KiB
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>

using ll = long long;
using namespace std;

ll lim(ll count, ll num, ll bs)
{
    ll MAXD = log(1e13) / log(bs);
    ll i, c, cc, ec, r, d[MAXD];

    c = ec = 0;

    for(i = 0; i < MAXD; i++)
    {
        cc = c;
        d[i] = 0;

        while(true)
        {
            if(i < MAXD - 1)
            {
                cc += ((MAXD - i - 1) * pow(bs, MAXD - i - 2));

                if(d[i] == num)
                    cc += pow(bs, MAXD - i - 1) - 1;
            }

            cc += ((ll) (d[i] + 1 == num)) + ec * pow(bs, MAXD - i - 1);

            if(d[i] < (bs - 1) && cc <= count)
            {
                c = cc;
                d[i]++;
            }
            else
                break;
        }

        ec += ((ll) (d[i] == num));
    }

    r = 0;

    for(i = 0; i < MAXD; i++)
        r += d[i] * pow(bs, MAXD - i - 1);

    return r;
}

ll lim0(ll count, ll bs)
{
    ll MAXD = log(1e13) / log(bs);
    ll i, j, c, cc, ec, r, d[MAXD];
    bool p;

    c = ec = 0;
    p = false;

    for(i = 0; i < MAXD; i++)
    {
        cc = c;
        d[i] = 0;

        while(true)
        {
            if(i < MAXD - 1)
            {
                if(!d[i] && !p)
                {
                    if(i == MAXD - 2)
                        cc++;
                    else
                    {
                        cc += ((MAXD - i - 2) * pow(bs, MAXD - i - 2));

                        for(j = 0; j < MAXD - i - 2; j++)
                            cc -= pow(bs, j);

                        cc += MAXD - i - 1;
                    }
                }
                else
                    cc += ((MAXD - i - 1) * pow(bs, MAXD - i - 2));

                if(!d[i] && p)
                    cc += pow(bs, MAXD - i - 1) - 1;
            }

            cc += ec * pow(bs, MAXD - i - 1);

            if(d[i] < (bs - 1) && cc <= count)
            {
                c = cc;
                d[i]++;
            }
            else
                break;
        }

        if(d[i])
            p = true;
        else if(p)
            ec++;
    }

    r = 0;

    for(i = 0; i < MAXD; i++)
        r += d[i] * pow(bs, MAXD - i - 1);

    return r;
}

int main()
{
    ll i, ans, K;

    cin >> K;

    vector<ll> d(K);

    for(i = 0; i < K; i++)
        cin >> d[i];

    ans = lim0(d[0], K);

    for(i = 1; i < K; i++)
        ans = min(lim(d[i], i, K), ans);

    cout << ans << endl;

    return 0;
}

SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1808 KiB
2Accepted3ms2064 KiB
3Accepted3ms2276 KiB
subtask225/25
4Accepted3ms2488 KiB
5Accepted3ms2724 KiB
6Accepted2ms2804 KiB
7Accepted3ms2808 KiB
8Accepted3ms2768 KiB
9Accepted3ms2968 KiB
10Accepted3ms3180 KiB
11Accepted3ms3296 KiB
12Accepted2ms3376 KiB
13Accepted2ms3532 KiB
14Accepted3ms3628 KiB
15Accepted3ms3628 KiB
16Accepted2ms3708 KiB
17Accepted3ms3840 KiB
18Accepted3ms3932 KiB
19Accepted2ms4020 KiB
subtask310/10
20Accepted3ms4148 KiB
21Accepted3ms4388 KiB
22Accepted2ms4476 KiB
23Accepted2ms4368 KiB
24Accepted2ms4368 KiB
subtask415/15
25Accepted3ms4368 KiB
26Accepted3ms4608 KiB
27Accepted2ms4688 KiB
28Accepted2ms4700 KiB
29Accepted2ms4788 KiB
30Accepted2ms4812 KiB
31Accepted2ms4784 KiB
32Accepted3ms4888 KiB
33Accepted2ms4768 KiB
subtask520/20
34Accepted2ms4884 KiB
35Accepted2ms4912 KiB
36Accepted2ms4772 KiB
37Accepted2ms4880 KiB
38Accepted3ms4892 KiB
39Accepted3ms4968 KiB
40Accepted3ms4964 KiB
41Accepted3ms4968 KiB
42Accepted2ms4968 KiB
subtask630/30
43Accepted3ms4964 KiB
44Accepted2ms4968 KiB
45Accepted3ms5124 KiB
46Accepted3ms5128 KiB
47Accepted3ms5292 KiB
48Accepted2ms5176 KiB
49Accepted3ms5300 KiB
50Accepted3ms5184 KiB
51Accepted3ms5208 KiB
52Accepted3ms5184 KiB
53Accepted2ms5180 KiB
54Accepted2ms5180 KiB
55Accepted3ms5176 KiB
56Accepted3ms5176 KiB
57Accepted3ms5180 KiB
58Accepted2ms5184 KiB
59Accepted3ms5180 KiB
60Accepted3ms5180 KiB