100702024-03-26 11:23:56Error42Kígyózó szavakcpp17Accepted 100/10052ms4644 KiB
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll h, n;
    cin >> h >> n;

    vector<vector<ll>> dp(h, vector<ll>(26, 0));

    for (int j = 0; j < 26; j++)
        dp[0][j] = 1;

    for (int i = 1; i < h; i++) {
        dp[i][0] = dp[i - 1][1] + 1;

        for (int j = 1; j < 25; j++)
            dp[i][j] = min((ll)INT_MAX, dp[i - 1][j - 1] + dp[i - 1][j + 1]) + 1;

        dp[i][25] = dp[i - 1][24] + 1;
    }

    while (n--) {
        ll k;
        cin >> k;

        string s = "";

        // first character
        int last = 0;

        while (dp[h - 1][last] < k) {
            k -= dp[h - 1][last];
            last++;
        }

        s.push_back('a' + last);

        for (int i = h - 2; i >= 0; i--) {
            if (k == 1)
                break;
            k--;

            vector<int> continuations;
            if (last > 0)
                continuations.push_back(last - 1);
            if (last < 25)
                continuations.push_back(last + 1);

            for (int j : continuations) {
                if (dp[i][j] >= k) {
                    s.push_back('a' + j);
                    last = j;
                    break;
                }

                k -= dp[i][j];
            }
        }

        cout << s << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/03ms1828 KiB
2Accepted0/03ms2020 KiB
3Accepted5/53ms2248 KiB
4Accepted5/53ms2440 KiB
5Accepted5/53ms2672 KiB
6Accepted5/53ms2744 KiB
7Accepted5/53ms3012 KiB
8Accepted5/54ms3092 KiB
9Accepted5/521ms3380 KiB
10Accepted5/517ms3348 KiB
11Accepted5/527ms3664 KiB
12Accepted5/528ms3684 KiB
13Accepted5/552ms4164 KiB
14Accepted6/613ms4024 KiB
15Accepted6/627ms4232 KiB
16Accepted6/617ms4276 KiB
17Accepted6/614ms4148 KiB
18Accepted7/717ms4256 KiB
19Accepted7/750ms4644 KiB
20Accepted7/718ms4336 KiB