結果

問題 No.329 全射
ユーザー kimiyukikimiyuki
提出日時 2016-10-24 15:47:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,705 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 85,212 KB
実行使用メモリ 7,320 KB
最終ジャッジ日時 2023-08-15 18:35:53
合計ジャッジ時間 3,549 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 28 ms
7,200 KB
testcase_14 AC 13 ms
6,944 KB
testcase_15 AC 19 ms
7,252 KB
testcase_16 AC 26 ms
7,244 KB
testcase_17 AC 21 ms
6,988 KB
testcase_18 AC 30 ms
7,212 KB
testcase_19 AC 28 ms
7,252 KB
testcase_20 AC 28 ms
7,320 KB
testcase_21 AC 24 ms
7,320 KB
testcase_22 AC 21 ms
7,008 KB
testcase_23 AC 23 ms
6,844 KB
testcase_24 AC 22 ms
7,116 KB
testcase_25 AC 25 ms
6,788 KB
testcase_26 AC 21 ms
7,168 KB
testcase_27 AC 18 ms
6,796 KB
testcase_28 AC 12 ms
5,888 KB
testcase_29 AC 17 ms
6,856 KB
testcase_30 AC 13 ms
5,884 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 15 ms
6,616 KB
testcase_33 AC 15 ms
6,720 KB
testcase_34 AC 15 ms
7,056 KB
testcase_35 AC 14 ms
6,848 KB
testcase_36 AC 16 ms
7,072 KB
testcase_37 AC 15 ms
6,676 KB
testcase_38 AC 15 ms
6,716 KB
testcase_39 AC 14 ms
6,800 KB
testcase_40 AC 14 ms
6,712 KB
testcase_41 AC 14 ms
7,128 KB
testcase_42 AC 15 ms
6,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
typedef long long ll;
using namespace std;
const int mod = 1e9+7;
ll second_kind_stirling(ll n, ll k) { // O(nk)
    assert (0 <= n and 0 <= k);
    if (n  < k) return 0;
    if (n == k) return 1;
    if (k == 0) return 0;
    static vector<vector<ll> > memo;
    if (memo.size() <= n) {
        int l = memo.size();
        memo.resize(n + 1);
        repeat_from (i,l,n+1) memo[i].resize(i);
    }
    if (memo[n][k]) return memo[n][k];
    return memo[n][k] = (second_kind_stirling(n-1, k-1) + k * second_kind_stirling(n-1, k) % mod) % mod;
}
ll fact(ll n) {
    static vector<ll> memo(1,1);
    if (memo.size() <= n) {
        int l = memo.size();
        memo.resize(n + 1);
        repeat_from (i,l,n+1) memo[i] = memo[i-1] * i % mod;
    }
    return memo[n];
}
int main() {
    int n, m; cin >> n >> m;
    vector<int> w(n); repeat (i,n) cin >> w[i];
    vector<vector<int> > g(n);
    repeat (k,m) {
        int i, j; cin >> i >> j; -- i; -- j;
        g[j].push_back(i); // inverse
    }
    ll ans = 0;
    repeat (i,n) {
        vector<bool> used(n);
        function<void (int)> go = [&](int j) {
            used[j] = true;
            for (int k : g[j]) if (not used[k]) {
                if (w[i] <= w[k]) {
                    go(k);
                }
            }
        };
        go(i);
        repeat (j,n) if (used[j]) {
            ans += fact(w[i]) * second_kind_stirling(w[j], w[i]) % mod;
        }
    }
    ans = (ans % mod + mod) % mod;
    cout << ans << endl;
    return 0;
}
0