結果

問題 No.78 クジ付きアイスバー
ユーザー MisterMister
提出日時 2020-04-22 10:02:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,261 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 79,308 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 08:40:29
合計ジャッジ時間 1,809 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

using lint = long long;
constexpr lint INF = 1LL << 40;

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

void solve() {
    int n;
    lint k;
    std::string s;
    std::cin >> n >> k >> s;

    auto dp = vec(40, vec(n, 0LL));
    for (int i = 0; i < n; ++i) {
        lint get = 0, cnt = 1;
        for (int j = 0; j < n; ++j) {
            ++get;
            cnt += int(s[(i + j) % n] - '0') - 1;
            if (cnt == 0) break;
        }
        dp[0][i] = (cnt > 0 ? INF : get);
    }

    for (int j = 1; j < 40; ++j) {
        for (int i = 0; i < n; ++i) {
            if (dp[j - 1][i] == INF) {
                dp[j][i] = INF;
            } else {
                int ni = (i + dp[j - 1][i]) % n;
                dp[j][i] = std::min(dp[j - 1][i] + dp[j - 1][ni], INF);
            }
        }
    }

    lint ans = 0;
    int i = 0;
    for (int j = 39; j >= 0; --j) {
        if (dp[j][i] > k) continue;
        ans += (1LL << j);
        k -= dp[j][i];
        (i += dp[j][i]) %= n;
    }

    std::cout << ans + (k > 0) << std::endl;
}

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

    solve();

    return 0;
}
0