52282023-04-23 09:27:13anonHázszámokcpp17Wrong answer 25/1003ms5016 KiB
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>

using ll = long long;
using namespace std;

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) * pow(bs, MAXD - i - 2));
                else
                {
                    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;
                    }

                    if(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;
}

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) * 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;
}

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
2Accepted3ms2008 KiB
3Accepted3ms2248 KiB
subtask20/25
4Accepted3ms2468 KiB
5Wrong answer3ms2744 KiB
6Accepted3ms2656 KiB
7Accepted3ms2660 KiB
8Accepted3ms2788 KiB
9Accepted3ms2996 KiB
10Wrong answer3ms3084 KiB
11Accepted3ms3328 KiB
12Accepted3ms3452 KiB
13Accepted3ms3596 KiB
14Accepted3ms3856 KiB
15Wrong answer3ms3712 KiB
16Accepted3ms3968 KiB
17Accepted3ms4144 KiB
18Accepted2ms4164 KiB
19Wrong answer2ms4312 KiB
subtask310/10
20Accepted3ms4476 KiB
21Accepted2ms4448 KiB
22Accepted2ms4452 KiB
23Accepted2ms4452 KiB
24Accepted3ms4572 KiB
subtask415/15
25Accepted3ms4568 KiB
26Accepted3ms4840 KiB
27Accepted2ms4780 KiB
28Accepted3ms4772 KiB
29Accepted3ms4776 KiB
30Accepted2ms4888 KiB
31Accepted2ms4892 KiB
32Accepted3ms4784 KiB
33Accepted2ms4788 KiB
subtask50/20
34Accepted3ms4784 KiB
35Accepted3ms4796 KiB
36Wrong answer3ms4900 KiB
37Wrong answer3ms4676 KiB
38Accepted3ms4696 KiB
39Accepted2ms4776 KiB
40Accepted2ms4776 KiB
41Accepted2ms4780 KiB
42Accepted2ms4780 KiB
subtask60/30
43Wrong answer3ms4668 KiB
44Wrong answer3ms4672 KiB
45Wrong answer2ms4784 KiB
46Accepted2ms4784 KiB
47Accepted2ms4676 KiB
48Wrong answer3ms4696 KiB
49Accepted3ms4668 KiB
50Accepted3ms4808 KiB
51Accepted2ms4888 KiB
52Accepted2ms4908 KiB
53Accepted2ms4904 KiB
54Accepted3ms4900 KiB
55Accepted3ms4932 KiB
56Accepted3ms4904 KiB
57Accepted3ms4908 KiB
58Accepted3ms4904 KiB
59Accepted3ms4908 KiB
60Wrong answer3ms5016 KiB