結果

問題 No.329 全射
ユーザー nebukuro09nebukuro09
提出日時 2018-04-17 10:18:23
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,385 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 118,552 KB
実行使用メモリ 19,380 KB
最終ジャッジ日時 2023-09-03 19:37:35
合計ジャッジ時間 4,838 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(71): Deprecation: use `{ }` for an empty statement, not `;`

ソースコード

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 ok = new bool[][](N, N);
    foreach (i; 0..N) ok[i][i] = true;

    foreach (i; 0..N) {
        auto max_k = new int[](N);
        auto q = new BinaryHeap!(Array!(Tuple!(int, int)), "a[1] < b[1]");
        q.insert(tuple(i, W[i]));
        
        while (!q.empty) {
            auto t = q.front;
            auto n = t[0];
            auto k = t[1];
            q.removeFront;
            if (k <= max_k[n]) continue;
            max_k[n] = k;
            if (k >= W[n]) ok[i][n] = true;
            
            foreach (m; G[n]) {
                auto nk = min(k, W[m]);
                if (nk <= max_k[m]) continue;
                q.insert(tuple(m, nk));
            }
        }
    }

    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;

    foreach (i; 0..N) {
        foreach (j; 0..N) {
            if (ok[i][j]) {
                foreach (k; 1..W[j]+1) {
                    long tmp = (W[j] - k) % 2 == 0 ? 1 : -1;;
                    tmp = tmp * powmod(k, W[i], MOD) % MOD;
                    tmp = tmp * comb(W[j], k) % MOD;
                    ans = ((ans + tmp) % MOD + MOD) % 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