結果

問題 No.767 配られたジャパリまん
ユーザー ikdikd
提出日時 2018-12-25 16:57:11
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 610 ms / 2,000 ms
コード長 1,801 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 91,272 KB
実行使用メモリ 15,388 KB
最終ジャッジ日時 2023-09-03 21:46:14
合計ジャッジ時間 3,471 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 74 ms
5,632 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 14 ms
4,376 KB
testcase_10 AC 41 ms
5,356 KB
testcase_11 AC 29 ms
4,380 KB
testcase_12 AC 20 ms
4,500 KB
testcase_13 AC 37 ms
4,880 KB
testcase_14 AC 31 ms
4,408 KB
testcase_15 AC 35 ms
4,376 KB
testcase_16 AC 25 ms
4,380 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 34 ms
4,876 KB
testcase_20 AC 610 ms
15,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;
  import core.bitop : popcnt;

  const mod = 10 ^^ 8 + 7;
  long powmod(long b, long e) {
    if (e == 0)
      return 1;
    else if (e == 1)
      return b;
    else if (e & 1)
      return b * powmod(b, e - 1) % mod;
    else
      return powmod(b * b % mod, e / 2);
  }

  int h, w, k;
  rd(h, w, k);
  struct P {
    int y, x, idx;
  }

  auto fac = new long[](h + w + 1), inv = new long[](h + w + 1);
  fac[0] = inv[0] = 1;
  foreach (i; 1 .. (h + w + 1)) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = powmod(fac[i], mod - 2);
  }
  long cmb(int n, int r) {
    if (r < 0 || n < r)
      return 0;
    else
      return fac[n] * inv[r] % mod * inv[n - r] % mod;
  }

  auto abi = new P[](k);
  foreach (i; 0 .. k) {
    int y, x;
    rd(y, x);
    abi[i] = P(y, x, i);
  }
  abi.sort!((p, q) => (p.y == q.y ? p.x < q.x : p.y < q.y));
  auto dp = new long[](1 << k);
  fill(dp, 1);
  foreach (bits; 0 .. (1 << k)) {
    int cur_x = 0, cur_y = 0;
    foreach (p; abi) {
      if (bits & (1 << p.idx)) {
        int dy = p.y - cur_y, dx = p.x - cur_x;
        dp[bits] = dp[bits] * cmb(dy + dx, dy) % mod;
        cur_y = p.y;
        cur_x = p.x;
      }
    }
    dp[bits] = dp[bits] * cmb((h - cur_y) + (w - cur_x), h - cur_y) % mod;
  }
  foreach (i; 0 .. k) {
    foreach (bits; 0 .. (1 << k)) {
      if ((bits & (1 << i)) == 0) {
        dp[bits ^ (1 << i)] = (dp[bits ^ (1 << i)] + (mod - dp[bits])) % mod;
      }
    }
  }
  foreach (bits, x; dp) {
    if (popcnt(bits) & 1)
      writeln(mod - x);
    else
      writeln(x);
  }
}

void rd(T...)(ref T x) {
  import std.stdio, std.string, std.conv;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}
0