結果

問題 No.140 みんなで旅行
ユーザー gigurururugigurururu
提出日時 2015-01-30 20:33:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 560 ms / 5,000 ms
コード長 1,268 bytes
コンパイル時間 1,410 ms
コンパイル使用メモリ 157,252 KB
実行使用メモリ 41,976 KB
最終ジャッジ日時 2023-09-05 08:23:47
合計ジャッジ時間 5,526 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 19 ms
5,296 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 548 ms
41,892 KB
testcase_12 AC 12 ms
4,672 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 560 ms
41,976 KB
testcase_15 AC 551 ms
41,696 KB
testcase_16 AC 185 ms
18,320 KB
testcase_17 AC 76 ms
10,428 KB
testcase_18 AC 427 ms
33,096 KB
testcase_19 AC 531 ms
36,248 KB
testcase_20 AC 59 ms
9,064 KB
testcase_21 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;

#define mp make_pair

ll mod=1000000007;

template <typename argT,typename retT> function<retT(argT)> memo(function<retT(argT)>f){
    map<argT,retT> m;
    return [=](argT n) mutable -> retT {
        return (m.find(n) != m.end()) ? m[n] : (m[n] = f(n));
    };
}

function<ll(pll)> combination_len = memo<pll,ll>(
    [&](pll arg)->ll{
        ll n=arg.first, k=arg.second;
        if ( k == 0 || n == k ){ return 1; }
        return (combination_len(mp(n-1,k-1))+combination_len(mp(n-1,k)))%mod;
    }
);

function<ll(pll)> p = memo<pll,ll>(
    [&](pll arg)->ll{
        ll n=arg.first, k=arg.second;
        if ( k == 1 ){ return 1; }
        if ( n == 1 ){ return 0; }
        return (p(mp(n-1,k-1))+k*p(mp(n-1,k)))%mod;
    }
);

function<ll(pll)> q = memo<pll,ll>(
    [&](pll arg)->ll{
        ll n=arg.first, k=arg.second;
        if ( k == 0 ){ return 1; }
        return q(mp(n,k-1))*n%mod*(n-1)%mod;
    }
);

int main(){
    ll n,ans;
    cin>>n;
    ans=0;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=i;j++){
            ans = (ans + combination_len(mp(n,i)) * p(mp(i,j)) % mod * q(mp(j,n-i)) ) % mod;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0