結果

問題 No.140 みんなで旅行
ユーザー kyunakyuna
提出日時 2019-08-19 17:57:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,082 ms / 5,000 ms
コード長 3,494 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 75,484 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 05:10:10
合計ジャッジ時間 21,101 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 37 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4,071 ms
5,376 KB
testcase_12 AC 20 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 4,082 ms
5,376 KB
testcase_15 AC 4,038 ms
5,376 KB
testcase_16 AC 914 ms
5,376 KB
testcase_17 AC 280 ms
5,376 KB
testcase_18 AC 2,719 ms
5,376 KB
testcase_19 AC 3,170 ms
5,376 KB
testcase_20 AC 190 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = (int)1e9 + 7;


//---------------------------------------------------------------
template<int MOD> struct ModInt { int x;
    explicit operator bool() const { return !!x; }
    ModInt(int v = 0) : x(v % MOD) { if (x < 0) x += MOD; }
    ModInt(long long v) : x(v % MOD) { if (x < 0) x += MOD; }
    ModInt &operator+=(const ModInt &r) { if ((x += r.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(const ModInt &r) { if ((x += MOD - r.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(const ModInt &r) { x = 1LL * x * r.x % MOD; return *this; }
    ModInt &operator/=(const ModInt &r) { return *this *= r.inv(); }
    ModInt operator-() const { return x ? ModInt(MOD - x) : ModInt(x); }
    ModInt operator+(const ModInt &r) const { return ModInt(*this) += r; }
    ModInt operator-(const ModInt &r) const { return ModInt(*this) -= r; }
    ModInt operator*(const ModInt &r) const { return ModInt(*this) *= r; }
    ModInt operator/(const ModInt &r) const { return ModInt(*this) /= r; }
    ModInt inv() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }
        return ModInt(u); } // x.pow(MOD-2)
    ModInt pow(long long k) const { ModInt r(1), a(x);
        while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
    bool operator==(const ModInt r) const { return x == r.x; }
    bool operator!=(const ModInt r) const { return x != r.x; }
    bool operator< (const ModInt r) const { return x <  r.x; }
    friend ostream& operator<<(ostream &os, const ModInt<MOD>& a) { return os << a.x; }
    friend istream& operator>>(istream &is, ModInt<MOD>& a) { return is >> a.x; }
};
template<typename T, int SZ> struct Comb { vector<T> _fac, _ifac, _inv;
    Comb() : _fac(SZ + 1), _ifac(SZ + 1), _inv(SZ + 1) {
        _fac[0] = _ifac[SZ] = _inv[0] = 1;
        for (int i = 1; i <= SZ; i++) _fac[i] = _fac[i - 1] * i;
        _ifac[SZ] /= _fac[SZ];
        for (int i = SZ - 1; i >= 0; i--) _ifac[i] = _ifac[i + 1] * (i + 1);
        for (int i = 1; i <= SZ; i++) _inv[i] = _ifac[i] * _fac[i - 1]; }
    T inv(int n) { return n < 0 ? T(0) : _inv[n]; }
    T fac(int n) { return n < 0 ? T(0) : _fac[n]; }
    T ifac(int n) { return n < 0 ? T(0) : _ifac[n]; }
    T P(int a, int b) { return (b < 0 || a < b) ? T(0) : _fac[a] * _ifac[a - b]; }
    T C(int a, int b) { return b < 0 ? T(0) : P(a, b) * _ifac[b]; }
    T H(int n, int k) { if (n < 0 || k < 0) return T(0);
        return k == 0 ? T(1) : C(n + k - 1, k); }
    T S(int n, int k) { T r = 0;
        for (int i = 0; i <= k; i++) {
            T t = C(k, i) * T(i).pow(n); r += ((k - i) & 1 ? -t : t);
        }
        return r * _ifac[k];
    }
    T B(int n, int k) {
        if (n == 0) return T(1);
        T r = 0; k = min(k, n);
        vector<T> dp(k + 1); dp[0] = T(1);
        for (int i = 1; i <= k; i++) dp[i] = dp[i - 1] + (i & 1 ? -_ifac[i] : _ifac[i]);
        for (int i = 1; i <= k; i++) r += T(i).pow(n) * _ifac[i] * dp[k - i];
        return r;
    }
};
typedef ModInt<MOD> mint;
//---------------------------------------------------------------


int main() {
    Comb<mint, 555> com;
    int n; cin >> n;
    mint ans = 0;
    for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) {
        ans += com.C(n, x) * com.S(x, y) * mint(y * (y - 1)).pow(n - x);
    }
    cout << ans << endl;
    return 0;
}
0