結果

問題 No.140 みんなで旅行
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-24 07:50:15
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 1,626 bytes
コンパイル時間 3,443 ms
コンパイル使用メモリ 145,512 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-04-29 04:48:31
合計ジャッジ時間 2,428 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,760 KB
testcase_01 AC 6 ms
5,888 KB
testcase_02 AC 6 ms
5,888 KB
testcase_03 AC 6 ms
5,760 KB
testcase_04 AC 6 ms
5,760 KB
testcase_05 AC 6 ms
5,760 KB
testcase_06 AC 6 ms
5,760 KB
testcase_07 AC 6 ms
5,888 KB
testcase_08 AC 6 ms
5,760 KB
testcase_09 AC 6 ms
5,760 KB
testcase_10 AC 6 ms
5,760 KB
testcase_11 AC 12 ms
5,760 KB
testcase_12 AC 6 ms
5,760 KB
testcase_13 AC 6 ms
5,888 KB
testcase_14 AC 12 ms
5,760 KB
testcase_15 AC 12 ms
5,760 KB
testcase_16 AC 8 ms
5,760 KB
testcase_17 AC 7 ms
5,760 KB
testcase_18 AC 11 ms
5,760 KB
testcase_19 AC 11 ms
5,760 KB
testcase_20 AC 6 ms
5,888 KB
testcase_21 AC 6 ms
5,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>

using namespace std;

const long long modc=1e9+7, MAX=555;
vector<long long> f, finv;

long long inv(long long x, long long e=modc-2){
    long long ans = 1;
    x %= modc;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * x) % modc;
        e = e >> 1LL;
        x = (x*x) % modc;
    }

    return ans;
}

void init(){
    f.resize(MAX+1); finv.resize(MAX+1);
    f[0] = 1;
    for (int i=1; i<=MAX; i++) f[i] = (f[i-1]*i) % modc;
    finv[MAX] = inv(f[MAX]);
    for (int i=MAX-1; i>=0; i--) finv[i] = (finv[i+1] * (i+1)) % modc;
}

long long C(long long n, long long k){
    if (n < k || k < 0) return 0;

    return (((f[n] * finv[k]) % modc) * finv[n-k]) % modc;
}

vector<vector<long long>> stirling;

void stirling_number(long long n, long long modc=1e9+7){
    stirling.resize(n+1);
    for (long long i=0; i<=n; i++){
        stirling[i].resize(n+1);
        stirling[i][0] = 0;
    }

    stirling[0][0] = 1;

    for (long long i=1; i <= n; i++){
        for (long long j=1; j <= i; j++){
            stirling[i][j] = (stirling[i-1][j-1] + (j*stirling[i-1][j]) % modc) % modc;
        }
    }
}

int main(){
    init();
    stirling_number(555);
    long long N, S=0;
    cin >> N;
    
    for (int i=1; i<=N; i++){
        for (int j=1; j<=i; j++){
            S += ((C(N, i)*stirling[i][j]) % modc) * inv(j*(j-1), N-i) % modc;
            S %= modc;
        }
    }

    cout << S << endl;

    return 0;
}
0