結果
問題 | No.140 みんなで旅行 |
ユーザー |
![]() |
提出日時 | 2019-06-17 16:23:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 8 ms / 5,000 ms |
コード長 | 2,230 bytes |
コンパイル時間 | 710 ms |
コンパイル使用メモリ | 96,012 KB |
実行使用メモリ | 5,760 KB |
最終ジャッジ日時 | 2024-11-27 17:43:18 |
合計ジャッジ時間 | 1,530 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cassert>#include <iostream>#include <iomanip>#include <string>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <algorithm>#include <numeric>#include <bitset>#define all(c) c.begin(), c.end()#define rall(c) c.rbegin(), c.rend()using namespace std;using ll = long long;using Pll = pair<ll, ll>;using Pii = pair<int, int>;constexpr ll MOD = 1000000007;constexpr int N_MAX = 556;ll fact[N_MAX], rfact[N_MAX];ll perm(ll n, ll r){return (fact[n] * rfact[r]) % MOD;}ll comb(ll n, ll r){return (perm(n, r) * rfact[n-r]) % MOD;}void init(ll n){fact[0] = fact[1] = 1;rfact[0] = rfact[1] = 1;for(int i=2;i<=n;++i) {fact[i] = (fact[i-1] * (ll)i) % MOD;rfact[i] = 1;ll k = MOD-2;ll a = fact[i];while(k > 0){if(k & 1){rfact[i] *= a;rfact[i] %= MOD;}a *= a;a %= MOD;k >>= 1;}}}class Stirling {public:int n, m; // ボールの個数,グループ数ll s[2*N_MAX][N_MAX];Stirling(int n, int m, bool is_the_second_kind=true): n(n), m(m) {if(is_the_second_kind) {for(int i=0;i<=n;++i) {s[i][0] = (i == 0)?1:0;if(i == 0) continue;for(int j=1;j<=m;++j) {s[i][j] = (s[i-1][j-1] + (s[i-1][j]*j) % MOD) % MOD;}}}}ll get(int i, int j) {return s[i][j];}};ll modpow(ll a, ll t) {ll ret = 1LL;while(t){if(t & 1LL){ret *= a;ret %= MOD;}a *= a;a %= MOD;t >>= 1;}return ret;}int main() {std::ios::sync_with_stdio(0); cin.tie(0);ll n;cin >> n;init(n);Stirling s = Stirling(n, n);ll ans = 0LL;for(ll x=1;x<=n;++x) {for(ll y=1;y<=x;++y) {ans += (((comb(n, x) * s.get(x, y)) % MOD) * modpow((y * (y-1)) % MOD, n-x)) % MOD;ans %= MOD;}}cout << ans << endl;}