結果

問題 No.1770 N言っちゃダメゲーム (6)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-02 13:40:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,822 bytes
コンパイル時間 838 ms
コンパイル使用メモリ 107,260 KB
実行使用メモリ 11,432 KB
最終ジャッジ日時 2023-09-04 15:12:04
合計ジャッジ時間 4,346 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 41 ms
11,368 KB
testcase_07 AC 41 ms
11,344 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 32 ms
10,364 KB
testcase_11 AC 32 ms
8,840 KB
testcase_12 AC 32 ms
8,868 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 46 ms
8,832 KB
testcase_29 AC 21 ms
5,556 KB
testcase_30 AC 40 ms
10,624 KB
testcase_31 AC 17 ms
5,676 KB
testcase_32 AC 48 ms
10,100 KB
testcase_33 AC 52 ms
11,432 KB
testcase_34 AC 53 ms
10,628 KB
testcase_35 AC 51 ms
11,140 KB
testcase_36 AC 50 ms
10,064 KB
testcase_37 AC 50 ms
10,052 KB
testcase_38 AC 49 ms
10,916 KB
testcase_39 AC 49 ms
10,312 KB
testcase_40 AC 49 ms
10,596 KB
testcase_41 AC 48 ms
10,392 KB
testcase_42 AC 49 ms
10,628 KB
testcase_43 AC 50 ms
10,048 KB
testcase_44 AC 33 ms
7,520 KB
testcase_45 AC 50 ms
10,104 KB
testcase_46 AC 56 ms
10,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


/*
  -2: all 0
  -1: all 1
*/
int[] solve(int N, int K) {
  auto dp = new int[N + 1];
  auto kss = new int[][N + 1];
  foreach (n; 0 .. N + 1) {
    if (kss[n].length == 0) {
      dp[n] = -2;
      foreach (k; 1 .. K + 1) {
        if (n + k <= N) {
          kss[n + k] ~= k;
        }
      }
    } else if (kss[n].length == 1) {
      const k = kss[n][0];
      dp[n] = k;
      if (n + k <= N) {
        kss[n + k] ~= k;
      }
    } else {
      dp[n] = -1;
    }
  }
  return dp;
}

void main() {
  debug {{
    enum lim = 20;
    foreach (k; 1 .. 10 + 1) {
      writeln("k = ", k);
      auto dp = new bool[][](lim + 1, k + 1);
      foreach (n; 0 .. lim + 1) {
        foreach (a; 0 .. k + 1) {
          foreach (b; 1 .. k + 1) if (a != b) {
            if (n - b >= 0 && !dp[n - b][b]) {
              dp[n][a] = true;
            }
          }
        }
      }
      foreach (n; 0 .. lim + 1) {
        writef("%2d: ", n);
        foreach (a; 0 .. k + 1) {
          write(dp[n][a] ? 1 : 0);
        }
        writeln;
      }
      const res = solve(lim, k);
      foreach (n; 0 .. lim + 1) {
        foreach (a; 0 .. k + 1) {
          assert(dp[n][a] == !(res[n] == -2 || res[n] == a));
        }
      }
    }
  }}
  
  try {
    for (; ; ) {
      const N = readInt() - 1;
      const K = readInt();
      
      const res = solve(N, K);
      debug {
        writeln(res);
      }
      bool win;
      foreach (k; 1 .. K + 1) {
        if (res[N - k] == -2 || res[N - k] == k) {
          win = true;
          writeln(k);
        }
      }
      if (!win) {
        writeln(0);
      }
    }
  } catch (EOFException e) {
  }
}
0