結果

問題 No.329 全射
ユーザー nebukuro09nebukuro09
提出日時 2018-04-17 13:10:03
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,622 ms / 2,000 ms
コード長 2,331 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 103,100 KB
実行使用メモリ 12,592 KB
最終ジャッジ日時 2023-09-03 19:40:45
合計ジャッジ時間 15,735 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,132 KB
testcase_01 AC 4 ms
11,584 KB
testcase_02 AC 4 ms
11,052 KB
testcase_03 AC 5 ms
11,012 KB
testcase_04 AC 5 ms
11,804 KB
testcase_05 AC 6 ms
11,568 KB
testcase_06 AC 5 ms
10,780 KB
testcase_07 AC 4 ms
11,764 KB
testcase_08 AC 4 ms
11,820 KB
testcase_09 AC 4 ms
12,140 KB
testcase_10 AC 4 ms
12,052 KB
testcase_11 AC 4 ms
11,796 KB
testcase_12 AC 5 ms
12,056 KB
testcase_13 AC 1,401 ms
12,556 KB
testcase_14 AC 1,145 ms
12,376 KB
testcase_15 AC 961 ms
12,392 KB
testcase_16 AC 1,102 ms
12,116 KB
testcase_17 AC 1,242 ms
11,564 KB
testcase_18 AC 1,233 ms
12,100 KB
testcase_19 AC 1,527 ms
12,140 KB
testcase_20 AC 1,352 ms
12,040 KB
testcase_21 AC 1,297 ms
11,652 KB
testcase_22 AC 1,622 ms
11,860 KB
testcase_23 AC 32 ms
11,644 KB
testcase_24 AC 31 ms
11,652 KB
testcase_25 AC 42 ms
11,652 KB
testcase_26 AC 33 ms
12,032 KB
testcase_27 AC 18 ms
11,180 KB
testcase_28 AC 9 ms
11,044 KB
testcase_29 AC 17 ms
11,104 KB
testcase_30 AC 13 ms
10,796 KB
testcase_31 AC 5 ms
11,572 KB
testcase_32 AC 13 ms
10,800 KB
testcase_33 AC 21 ms
11,280 KB
testcase_34 AC 20 ms
12,592 KB
testcase_35 AC 25 ms
11,264 KB
testcase_36 AC 22 ms
11,040 KB
testcase_37 AC 19 ms
11,856 KB
testcase_38 AC 22 ms
11,596 KB
testcase_39 AC 21 ms
11,152 KB
testcase_40 AC 25 ms
11,336 KB
testcase_41 AC 19 ms
11,244 KB
testcase_42 AC 27 ms
11,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    immutable long MOD = 10^^9 + 7;
    
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto W = readln.split.map!(to!int).array;
    auto G = new int[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
    }

    auto D = new int[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) D[i][j] = i == j ? W[i] : -1;
    foreach (i; 0..N) foreach (j; G[i]) D[i][j] = min(W[i], W[j]);
    foreach (i; 0..N) foreach (j; 0..N) foreach (k; 0..N) D[j][k] = max(D[j][k], min(D[j][i], D[i][k]));

    auto ok = new bool[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) ok[i][j] = D[i][j] >= W[j];

    immutable int MAX = 1001;
    
    auto modinv = new long[](MAX);
    modinv[0] = modinv[1] = 1;
    foreach(i; 2..MAX) {
        modinv[i] = modinv[MOD % i] * (MOD - MOD / i) % MOD;
    }

    auto f_mod = new long[](MAX);
    auto f_modinv = new long[](MAX);
    f_mod[0] = f_mod[1] = 1;
    f_modinv[0] = f_modinv[1] = 1;

    foreach(i; 2..MAX) {
        f_mod[i] = (i * f_mod[i-1]) % MOD;
        f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
    }

    long comb(int n, int k) {
        return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
    }

    long ans = 0;
    auto mem = new long[][](1001, 1001);
    foreach (i; 0..1001) mem[i].fill(-1);

    foreach (i; 0..N) {
        foreach (j; 0..N) {
            if (!ok[i][j]) continue;
            if (mem[W[i]][W[j]] == -1) {
                mem[W[i]][W[j]] = 0;
                foreach (k; 1..W[j]+1) {
                    long tmp = 1;
                    if ((W[j] - k) % 2 == 1) tmp = -1;
                    tmp = tmp * powmod(k, W[i], MOD) % MOD;
                    tmp = tmp * comb(W[j], k) % MOD;
                    mem[W[i]][W[j]] = ((mem[W[i]][W[j]] + tmp) % MOD + MOD) % MOD;
                }
            }
            ans = (ans + mem[W[i]][W[j]]) % MOD;
        }
    }

    ans.writeln;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0