結果

問題 No.329 全射
ユーザー nebukuro09nebukuro09
提出日時 2018-04-17 12:20:59
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,447 ms / 2,000 ms
コード長 4,524 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 114,016 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-06-13 00:33:31
合計ジャッジ時間 13,526 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,112 KB
testcase_01 AC 6 ms
10,112 KB
testcase_02 AC 5 ms
10,112 KB
testcase_03 AC 7 ms
10,240 KB
testcase_04 AC 7 ms
10,112 KB
testcase_05 AC 6 ms
10,112 KB
testcase_06 AC 7 ms
10,240 KB
testcase_07 AC 5 ms
10,112 KB
testcase_08 AC 5 ms
10,112 KB
testcase_09 AC 5 ms
10,112 KB
testcase_10 AC 6 ms
10,112 KB
testcase_11 AC 5 ms
10,112 KB
testcase_12 AC 7 ms
10,112 KB
testcase_13 AC 1,259 ms
10,880 KB
testcase_14 AC 1,001 ms
10,368 KB
testcase_15 AC 855 ms
10,496 KB
testcase_16 AC 960 ms
10,752 KB
testcase_17 AC 1,103 ms
10,624 KB
testcase_18 AC 1,080 ms
10,752 KB
testcase_19 AC 1,348 ms
10,880 KB
testcase_20 AC 1,195 ms
10,624 KB
testcase_21 AC 1,170 ms
10,752 KB
testcase_22 AC 1,447 ms
10,496 KB
testcase_23 AC 26 ms
10,368 KB
testcase_24 AC 25 ms
10,368 KB
testcase_25 AC 32 ms
10,624 KB
testcase_26 AC 27 ms
10,496 KB
testcase_27 AC 16 ms
10,240 KB
testcase_28 AC 8 ms
10,112 KB
testcase_29 AC 16 ms
10,240 KB
testcase_30 AC 14 ms
10,368 KB
testcase_31 AC 7 ms
10,112 KB
testcase_32 AC 13 ms
10,240 KB
testcase_33 AC 18 ms
10,496 KB
testcase_34 AC 18 ms
10,368 KB
testcase_35 AC 21 ms
10,496 KB
testcase_36 AC 18 ms
10,368 KB
testcase_37 AC 17 ms
10,368 KB
testcase_38 AC 19 ms
10,496 KB
testcase_39 AC 18 ms
10,368 KB
testcase_40 AC 20 ms
10,368 KB
testcase_41 AC 16 ms
10,368 KB
testcase_42 AC 24 ms
10,368 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;

    Scanner sc = new Scanner(stdin);
    int N, M; sc.read(N); sc.read(M);
    auto W = new int[](N);
    foreach (i; 0..N) sc.read(W[i]);
    auto G = new int[][](N);
    foreach (_; 0..M) {
        int u, v; sc.read(u); sc.read(v);
        G[u-1] ~= v-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) {
                long tmp2 = 0;
                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;
                    tmp2 = ((tmp2 + tmp) % MOD + MOD) % MOD;
                }
                mem[W[i]][W[j]] = tmp2;
            }
            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;
}

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                 
                 
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}
0