52312023-04-23 10:12:06anonHázszámokcpp17Wrong answer 25/1003ms5128 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(1e12) / 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(1e12) / 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
1Accepted3ms1812 KiB
2Accepted3ms2052 KiB
3Accepted2ms2088 KiB
subtask20/25
4Accepted3ms2216 KiB
5Wrong answer2ms2576 KiB
6Accepted3ms2736 KiB
7Accepted2ms2816 KiB
8Accepted3ms2952 KiB
9Accepted3ms3088 KiB
10Wrong answer2ms3172 KiB
11Accepted2ms3108 KiB
12Accepted3ms3248 KiB
13Accepted3ms3368 KiB
14Accepted2ms3344 KiB
15Wrong answer3ms3312 KiB
16Accepted3ms3320 KiB
17Accepted3ms3556 KiB
18Accepted2ms3636 KiB
19Wrong answer3ms3776 KiB
subtask310/10
20Accepted3ms3860 KiB
21Accepted3ms3992 KiB
22Accepted3ms4124 KiB
23Accepted3ms4216 KiB
24Accepted3ms4308 KiB
subtask415/15
25Accepted3ms4824 KiB
26Accepted2ms4536 KiB
27Accepted2ms4780 KiB
28Accepted3ms4668 KiB
29Accepted3ms4668 KiB
30Accepted3ms4796 KiB
31Accepted2ms4880 KiB
32Accepted2ms4760 KiB
33Accepted2ms4884 KiB
subtask50/20
34Accepted3ms4880 KiB
35Accepted2ms4884 KiB
36Wrong answer3ms4880 KiB
37Wrong answer2ms4884 KiB
38Accepted3ms5128 KiB
39Accepted3ms5104 KiB
40Accepted3ms4964 KiB
41Accepted3ms5092 KiB
42Accepted3ms4972 KiB
subtask60/30
43Wrong answer3ms4972 KiB
44Wrong answer2ms5080 KiB
45Wrong answer3ms4968 KiB
46Accepted3ms4968 KiB
47Accepted3ms4964 KiB
48Wrong answer3ms4968 KiB
49Accepted3ms4968 KiB
50Accepted3ms4968 KiB
51Accepted3ms5084 KiB
52Accepted3ms4972 KiB
53Accepted2ms4968 KiB
54Accepted3ms4996 KiB
55Accepted3ms4968 KiB
56Accepted3ms4964 KiB
57Accepted3ms4968 KiB
58Accepted3ms4964 KiB
59Wrong answer3ms4968 KiB
60Wrong answer3ms4964 KiB