結果

問題 No.767 配られたジャパリまん
ユーザー nebukuro09nebukuro09
提出日時 2018-12-15 17:35:05
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,890 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 114,552 KB
実行使用メモリ 23,836 KB
最終ジャッジ日時 2023-09-03 21:41:50
合計ジャッジ時間 6,792 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
6,152 KB
testcase_01 AC 108 ms
5,640 KB
testcase_02 AC 108 ms
5,628 KB
testcase_03 AC 108 ms
5,664 KB
testcase_04 AC 109 ms
6,196 KB
testcase_05 AC 208 ms
7,748 KB
testcase_06 AC 108 ms
6,168 KB
testcase_07 AC 108 ms
5,624 KB
testcase_08 AC 108 ms
5,616 KB
testcase_09 AC 109 ms
6,148 KB
testcase_10 AC 158 ms
7,284 KB
testcase_11 AC 111 ms
6,688 KB
testcase_12 AC 111 ms
6,664 KB
testcase_13 AC 157 ms
7,196 KB
testcase_14 AC 110 ms
6,200 KB
testcase_15 AC 108 ms
5,900 KB
testcase_16 AC 111 ms
6,684 KB
testcase_17 AC 108 ms
5,664 KB
testcase_18 AC 108 ms
5,904 KB
testcase_19 AC 114 ms
6,992 KB
testcase_20 AC 1,890 ms
23,836 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, core.stdc.string;

immutable long MOD = 10^^8 + 7;
immutable long MAX = 2 * 10^^5 + 10;
long[] F, G;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto K = s[2];
    auto P = K.iota.map!(i => readln.split.map!(to!int).array ~ i).array;
    P.sort();

    F = new long[](MAX+1);
    G = new long[](MAX+1);
    F[0] = 1;
    foreach (i; 1..MAX+1) F[i] = F[i-1] * i % MOD;
    foreach (i; 0..MAX+1) G[i] = powmod(F[i], MOD-2, MOD);


    auto all = new long[](1<<K); // all[S]: 集合Sに含まれる座標をすべて通る経路数
    auto any = new long[](1<<K); // any[S]: 集合Sに含まれる座標のうち少なくとも1つは通る経路数
    auto dp = new long[][](K+1, K+1);
    all[0] = comb(H+W, H);

    outer: foreach (mask; 1..(1<<K)) {
        auto A = [0, 0, -1] ~ (K.iota.filter!(i => mask & (1 << P[i][2])).map!(i => P[i]).array) ~ [H, W, -1];
        long tmp = 1;
        foreach (i; 0..A.length.to!int-1) {
            if (A[i][1] > A[i+1][1]) {
                continue outer;
            }
            tmp *= comb(A[i+1][0] - A[i][0] + A[i+1][1] - A[i][1], A[i+1][0] - A[i][0]);
            tmp %= MOD;
        }
        all[mask] = tmp;
    }

    foreach (mask; 0..(1<<K)) {
        int sign = mask.popcnt % 2 ? -1 : 1;
        any[mask] = sign * all[mask] % MOD;
    }

    foreach (i; 0..K) {
        foreach (mask; 0..(1<<K)) {
            if (mask & (1 << i)) {
                int sign = mask.popcnt % 2 ? 1 : -1;
                any[mask] += any[mask ^ (1 << i)];
                any[mask] %= MOD;
            }
	}
    }

    any.each!(a => writeln((a % MOD + MOD) % MOD));
}

long comb(long n, long r) {
    if (n < r) return 0;
    return F[n] * G[n-r] % MOD * G[r] % MOD;
}

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