結果

問題 No.1684 Find Brackets
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-14 01:16:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,296 bytes
コンパイル時間 6,260 ms
コンパイル使用メモリ 220,952 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2023-09-12 06:20:25
合計ジャッジ時間 7,738 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
10,932 KB
testcase_01 AC 15 ms
10,932 KB
testcase_02 AC 52 ms
26,624 KB
testcase_03 AC 15 ms
10,828 KB
testcase_04 AC 16 ms
10,844 KB
testcase_05 AC 15 ms
11,056 KB
testcase_06 AC 15 ms
10,940 KB
testcase_07 AC 46 ms
25,908 KB
testcase_08 AC 43 ms
24,060 KB
testcase_09 AC 33 ms
22,324 KB
testcase_10 AC 24 ms
14,696 KB
testcase_11 AC 21 ms
13,292 KB
testcase_12 AC 35 ms
19,096 KB
testcase_13 AC 44 ms
24,168 KB
testcase_14 AC 53 ms
26,624 KB
testcase_15 AC 38 ms
26,556 KB
testcase_16 AC 52 ms
26,504 KB
testcase_17 AC 53 ms
26,552 KB
testcase_18 AC 51 ms
26,364 KB
testcase_19 AC 15 ms
11,044 KB
testcase_20 AC 15 ms
10,956 KB
testcase_21 AC 39 ms
26,488 KB
testcase_22 AC 38 ms
26,612 KB
testcase_23 AC 38 ms
26,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validator
#include "testlib.h"
#include <cassert>
#include <iostream>
#include <vector>
#include <atcoder/modint>
using namespace std;

template <typename modint> struct acl_fac {
    std::vector<modint> facs, facinvs;
    acl_fac(int N) {
        assert(-1 <= N and N < modint::mod());
        facs.resize(N + 1, 1);
        for (int i = 1; i <= N; i++) facs[i] = facs[i - 1] * i;
        facinvs.assign(N + 1, facs.back().inv());
        for (int i = N; i > 0; i--) facinvs[i - 1] = facinvs[i] * i;
    }
    modint ncr(int n, int r) const {
        if (n < 0 or r < 0 or n < r) return 0;
        return facs[n] * facinvs[r] * facinvs[n - r];
    }
    modint operator[](int i) const { return facs[i]; }
    modint finv(int i) const { return facinvs[i]; }
};
using mint = atcoder::modint1000000007;
acl_fac<mint> fac(1000000);

int main(int argc, char **argv) {
    registerValidation(argc, argv);
    int N = inf.readInt(1, 1000000);
    inf.readSpace();
    int M = inf.readInt(0, N);
    inf.readEoln();
    inf.readEof();

    acl_fac<mint> fac(N);
    vector<mint> ncrs(N + 1);
    for (int i = 0; i < int(ncrs.size()); i++) ncrs[i] = fac.ncr(N, i);
    vector<mint> ncr_cums(ncrs.size() + 1);
    for (int i = 0; i < int(ncrs.size()); i++) ncr_cums[i + 1] = ncr_cums[i] + ncrs[i];

    mint ret = 0;

    auto add_closed = [&](int l, int r) {
        if (l > r) return;
        l = max(l, 0);
        r = min(r, N);
        ret += ncr_cums[r + 1] - ncr_cums[l];
    };

    for (int h = 1; h <= N; h++) {
        if (M - (N - M) >= h) {
            add_closed(M, N);
        } else {
            int l = h + (N - h + 1) / 2;
            add_closed(l, N);
            if ((N - h) % 2) {
                add_closed(l, l - 1 + l - M);
            } else {
                add_closed(l + 1, l + l - M);
            }
        }
    }
    const int K = N - M;
    for (int h = 1; h <= K; h++) {
        const int l = h + (N - h + 1) / 2;
        if (l <= K) {
            add_closed(l, K);
            if ((N - h) % 2) {
                add_closed(l, N);
            } else {
                add_closed(l + 1, N);
            }
        } else {
            add_closed(h + N - K, N);
        }
    }
    for (int i = M; i <= N; i++) ret += fac.ncr(N, i) * N;
    cout << ret.val() << '\n';
}
0