結果

問題 No.140 みんなで旅行
ユーザー codershifthcodershifth
提出日時 2015-08-15 11:14:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,840 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 151,748 KB
実行使用メモリ 8,148 KB
最終ジャッジ日時 2023-09-25 12:01:21
合計ジャッジ時間 3,543 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 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,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 22 ms
7,888 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 22 ms
8,148 KB
testcase_15 AC 21 ms
7,984 KB
testcase_16 AC 9 ms
4,976 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 17 ms
6,736 KB
testcase_19 AC 18 ms
7,216 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

const int Mod = (int)(1e+9)+7;
long long  mpow(long long a, int k) {
        long long  b = 1;
        while (k > 0)
        {
            if (k & 1)
                b = (b*a)%Mod;
            a = (a*a)%Mod;
            k >>= 1;
        }
        return b;
}
class TravelTogether {
public:
    void solve(void) {
            //
            // F(x,y) := 夫婦が共に同じグループに入る夫婦が x 組、グループが y となる組み合わせ
            //
            // とすると
            //          | 1 (x==y==0)
            // F(x,y) = | 0 (x < y or  y <= 0) (x < y で 0 なのは条件から一つのグループには1組以上の夫婦が入るため)
            //          | F(x-1,y-1) + y*F(x-1,y) (otherwise)
            //            ~~~~~~~~~~~~~~~~~~~~~~~
            // (x組目が1グループを構成する)+(x組目がすでにある y グループのどれかに含まれる)
            //
            // N 組中 x 組の夫婦が同じグループで、計 y グループ作る組み合わせは
            //
            //  nCx * F(x,y) * (y*(y-1))^(N-x)
            //                 ~~~~~~~~~~~~~~~
            //                  残り N-x 組は夫婦別々のはずなのでそれぞれ yC1*(y-1)C1 となる
            int N;
            cin>>N;
            vector<vector<ll>> F(N+1,vector<ll>(N+1,-1));
            vector<vector<ll>> C(N+1,vector<ll>(N+1,0));

            // nCr を事前計算
            REP(n,N)
            {
                C[n][0] = 1;
                REP(r,N)
                    (C[n+1][r+1] = (C[n][r+1] + C[n][r]) % Mod) %= Mod;
            }

            // F を計算
            function<ll(int,int)> f = [&](int x, int y) {
                if (F[x][y] >= 0)
                    return F[x][y];
                if (x==0 && y==0)
                    return F[x][y] = 1;
                if (x < y || y <= 0)
                    return F[x][y] = 0;
                return F[x][y] = (f(x-1,y-1) + y*f(x-1,y)% Mod) % Mod;
            };
            // 事前計算
            REP(x,N+1)
            REP(y,N+1)
                f(x,y);

            ll res = 0;
            REP(x,N+1)
            REP(y,N+1)
            {
                ll tmp = C[N][x];
                (tmp *= F[x][y]) %= Mod;
                (tmp *= mpow(y*(y-1)%Mod, N-x)) %= Mod;
                (res += tmp) %= Mod;
            }
            cout<<res<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new TravelTogether();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0