52302023-04-23 10:08:54anonHázszámokcpp17Wrong answer 25/1003ms5068 KiB
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>

using ll = long long;
using namespace std;

ll my_pow(ll bs, ll pw)
{
    ll i, r;

    r = 1;

    for(i = 0; i < pw; i++)
        r *= bs;

    return r;
}

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

    ec = 0;
    c = 0;
    p = false;

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

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

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

                        cc += MAXD - i - 1;
                    }

                    if(p)
                        cc += my_pow(bs, MAXD - i - 1) - 1;
                }
            }

            cc += ec * my_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] * my_pow(bs, MAXD - i - 1);

    return r;
}

ll lim(ll count, ll num, ll bs)
{
    ll MAXD = log(1e15) / 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) * my_pow(bs, MAXD - i - 2));

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

            cc += ((ll) (d[i] + 1 == num)) + ec * my_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] * my_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
1Accepted3ms1816 KiB
2Accepted3ms2056 KiB
3Accepted3ms2212 KiB
subtask20/25
4Accepted3ms2428 KiB
5Wrong answer2ms2512 KiB
6Accepted2ms2512 KiB
7Accepted2ms2640 KiB
8Accepted3ms2648 KiB
9Accepted3ms2900 KiB
10Wrong answer3ms2808 KiB
11Accepted3ms2936 KiB
12Accepted3ms3148 KiB
13Accepted3ms3260 KiB
14Accepted3ms3264 KiB
15Wrong answer2ms3380 KiB
16Accepted3ms3620 KiB
17Accepted2ms3508 KiB
18Accepted3ms3476 KiB
19Wrong answer3ms3724 KiB
subtask310/10
20Accepted3ms3572 KiB
21Accepted3ms3576 KiB
22Accepted3ms3700 KiB
23Accepted3ms3916 KiB
24Accepted3ms3992 KiB
subtask415/15
25Accepted3ms4124 KiB
26Accepted3ms4336 KiB
27Accepted2ms4420 KiB
28Accepted3ms4428 KiB
29Accepted3ms4560 KiB
30Accepted2ms4628 KiB
31Accepted2ms4632 KiB
32Accepted2ms4628 KiB
33Accepted3ms4752 KiB
subtask50/20
34Accepted3ms4752 KiB
35Accepted2ms4632 KiB
36Wrong answer3ms4656 KiB
37Wrong answer3ms4636 KiB
38Accepted3ms4660 KiB
39Accepted2ms4732 KiB
40Accepted2ms4740 KiB
41Accepted3ms4756 KiB
42Accepted3ms4856 KiB
subtask60/30
43Wrong answer3ms4948 KiB
44Wrong answer3ms5052 KiB
45Wrong answer3ms4940 KiB
46Accepted3ms5056 KiB
47Accepted3ms4940 KiB
48Wrong answer3ms4944 KiB
49Accepted2ms4940 KiB
50Accepted3ms4944 KiB
51Accepted2ms4944 KiB
52Accepted2ms4944 KiB
53Accepted3ms4940 KiB
54Accepted2ms4944 KiB
55Accepted3ms4968 KiB
56Accepted2ms5052 KiB
57Accepted2ms4948 KiB
58Accepted2ms4980 KiB
59Accepted2ms4956 KiB
60Wrong answer2ms5068 KiB