結果

問題 No.767 配られたジャパリまん
ユーザー ikdikd
提出日時 2018-12-25 17:56:11
言語 D
(dmd 2.106.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,867 bytes
コンパイル時間 651 ms
コンパイル使用メモリ 90,964 KB
実行使用メモリ 15,436 KB
最終ジャッジ日時 2023-09-03 21:46:27
合計ジャッジ時間 4,905 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 179 ms
6,712 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 14 ms
4,380 KB
testcase_10 AC 83 ms
6,200 KB
testcase_11 AC 31 ms
4,656 KB
testcase_12 AC 23 ms
4,380 KB
testcase_13 AC 80 ms
6,224 KB
testcase_14 AC 31 ms
4,820 KB
testcase_15 AC 36 ms
4,840 KB
testcase_16 AC 27 ms
4,376 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 39 ms
5,896 KB
testcase_20 TLE -
権限があれば一括ダウンロードができます

ソースコード

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);
  }
  auto dp = new long[](1 << k);
  fill(dp, 1);
  foreach (bits; 0 .. (1 << k)) {
    P[] pts;
    foreach (i; 0 .. k) {
      if (bits & (1 << i))
        pts ~= P(abi[i].y, abi[i].x, i);
    }
    pts.sort!((p, q) => (p.y == q.y ? p.x < q.x : p.y < q.y));
    int cur_x = 0, cur_y = 0;
    foreach (p; pts) {
      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