結果

問題 No.506 限られたジャパリまん
ユーザー te-shte-sh
提出日時 2018-01-10 14:13:53
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 1,645 ms
コンパイル使用メモリ 85,632 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 12:55:34
合計ジャッジ時間 2,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 28 ms
4,376 KB
testcase_09 AC 17 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 23 ms
4,380 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 18 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.bitmanip;  // BitArray

void read3(S,T,U)(ref S a,ref T b,ref U c){auto r=readln.splitter;a=r.front.to!S;r.popFront;b=r.front.to!T;r.popFront;c=r.front.to!U;}
void read4(S,T,U,V)(ref S a,ref T b,ref U c,ref V d){auto r=readln.splitter;a=r.front.to!S;r.popFront;b=r.front.to!T;r.popFront;c=r.front.to!U;r.popFront;d=r.front.to!V;r.popFront;}

const mod = 10^^9+7;

void main()
{
  int h, w, k, p; read4(h, w, k, p);

  struct Friend { int x, y; string name; }
  auto f = new Friend[](k);
  foreach (i; 0..k) {
    int x, y; string name; read3(x, y, name);
    f[i] = Friend(x, y, name);
  }

  auto maxR = 0L, maxI = 0;
  foreach (i; 0..1<<k) {
    if (i.popcnt != p) continue;
    auto fm = new bool[][](h+1, w+1);
    foreach (j; 0..k)
      if (!i.bitTest(j)) fm[f[j].x][f[j].y] = true;

    auto rm = new long[][](h+1, w+1);
    rm[0][0] = 1;
    foreach (x; 0..h+1)
      foreach (y; 0..w+1) {
        if (fm[x][y]) continue;
        if (x > 0) rm[x][y] += rm[x-1][y];
        if (y > 0) rm[x][y] += rm[x][y-1];
      }

    if (rm[h][w] > maxR) {
      maxR = rm[h][w];
      maxI = i;
    }
  }

  writeln(maxR % mod);
  foreach (j; 0..k)
    if (maxI.bitTest(j))
      writeln(f[j].name);
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
0