52292023-04-23 10:06:34anonHázszámokcpp17Wrong answer 0/1003ms3956 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(1e20) / 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
2Accepted3ms2052 KiB
3Accepted3ms2540 KiB
subtask20/25
4Wrong answer3ms2664 KiB
5Wrong answer3ms2664 KiB
6Wrong answer3ms2708 KiB
7Wrong answer3ms2680 KiB
8Accepted3ms2808 KiB
9Accepted3ms3048 KiB
10Wrong answer3ms3104 KiB
11Wrong answer2ms3108 KiB
12Wrong answer3ms3108 KiB
13Accepted3ms3232 KiB
14Accepted3ms3508 KiB
15Wrong answer3ms3448 KiB
16Accepted3ms3572 KiB
17Accepted3ms3640 KiB
18Accepted3ms3524 KiB
19Wrong answer3ms3460 KiB
subtask30/10
20Wrong answer3ms3580 KiB
21Wrong answer3ms3604 KiB
22Wrong answer3ms3460 KiB
23Accepted3ms3488 KiB
24Wrong answer3ms3472 KiB
subtask40/15
25Wrong answer3ms3468 KiB
26Wrong answer3ms3584 KiB
27Wrong answer2ms3464 KiB
28Accepted3ms3584 KiB
29Accepted3ms3456 KiB
30Accepted3ms3580 KiB
31Accepted3ms3456 KiB
32Accepted3ms3460 KiB
33Accepted3ms3464 KiB
subtask50/20
34Accepted2ms3472 KiB
35Accepted3ms3584 KiB
36Wrong answer3ms3708 KiB
37Wrong answer3ms3908 KiB
38Accepted3ms3920 KiB
39Accepted3ms3808 KiB
40Accepted3ms3808 KiB
41Accepted3ms3808 KiB
42Accepted3ms3804 KiB
subtask60/30
43Wrong answer3ms3956 KiB
44Wrong answer3ms3804 KiB
45Wrong answer3ms3932 KiB
46Accepted3ms3808 KiB
47Accepted3ms3804 KiB
48Wrong answer3ms3804 KiB
49Accepted3ms3812 KiB
50Accepted3ms3804 KiB
51Accepted3ms3932 KiB
52Wrong answer3ms3808 KiB
53Wrong answer3ms3808 KiB
54Wrong answer3ms3804 KiB
55Accepted3ms3808 KiB
56Accepted3ms3836 KiB
57Accepted3ms3812 KiB
58Accepted3ms3808 KiB
59Accepted3ms3808 KiB
60Wrong answer3ms3808 KiB