結果

問題 No.823 Many Shifts Easy
ユーザー tottoripapertottoripaper
提出日時 2019-04-27 17:41:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 2,012 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 177,504 KB
実行使用メモリ 6,128 KB
最終ジャッジ日時 2023-08-19 05:30:12
合計ジャッジ時間 3,773 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
6,128 KB
testcase_01 AC 11 ms
6,092 KB
testcase_02 AC 11 ms
6,104 KB
testcase_03 AC 15 ms
6,052 KB
testcase_04 AC 11 ms
6,108 KB
testcase_05 AC 13 ms
6,044 KB
testcase_06 AC 16 ms
5,948 KB
testcase_07 AC 11 ms
5,996 KB
testcase_08 AC 16 ms
6,052 KB
testcase_09 AC 11 ms
6,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

template <typename T>
std::tuple<T, T, T> extgcd(T a, T b){
    T s1 = 1, t1 = 0, s2 = 0, t2 = 1;
    while(b != 0){
        std::tie(s1, t1, s2, t2) = std::make_tuple(s2, t2, s1 - (a / b) * s2, t1 - (a / b) * t2);
        std::tie(a, b) = std::make_tuple(b, a % b);
    }
    return std::make_tuple(s1, t1, a);
}

// 注意: a と mod が互いに素である必要がある
template <typename T>
T mod_inverse(T a, T mod){
    T b;
    std::tie(b, std::ignore, std::ignore) = extgcd(a, mod);
    if(b < 0){b += mod;}
    return b;
}

template <typename T>
struct Combinatorics{
    T modulo;
    std::vector<T> fact, inv_fact;

    Combinatorics(T max, T modulo) : modulo(modulo), fact(max + 1), inv_fact(max + 1) {
        fact[0] = 1;
        for(T i=1;i<=max;++i){
            fact[i] = i * fact[i - 1] % modulo;
        }

        inv_fact[max] = mod_inverse(fact[max], modulo);
        for(T i=max;i>0;--i){
            inv_fact[i - 1] = i * inv_fact[i] % modulo;
        }
    }

    T nPk(T n, T k){
        if(n < k){return 0;}
        return fact[n] * inv_fact[n - k] % modulo;
    }

    T nCk(T n, T k){
        if(n < k){return 0;}
        return fact[n] * inv_fact[k] % modulo * inv_fact[n - k] % modulo;
    }

    T nHk(T n, T k){
        if(n == 0 && k == 0){return 1;}
        return nCk(n + k - 1, k);
    }
};

constexpr ll MOD = 1'000'000'007;
Combinatorics<ll> comb(200100, MOD);
int N, K;

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

    std::cin >> N >> K;

    ll cnt = 0;

    for(int i=1;i<=N;++i){
        cnt = (cnt + comb.nPk(N - 1, K) * i % MOD) % MOD;

        if(i < N){
            cnt = (cnt + comb.nCk(K, 2) * comb.nPk(N - 2, K - 2) % MOD * i % MOD) % MOD;
        }
    }

    std::cout << cnt << std::endl;
}
0