結果

問題 No.824 Many Shifts Hard
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-26 22:35:47
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 656 ms / 2,000 ms
コード長 3,179 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 106,456 KB
実行使用メモリ 16,944 KB
最終ジャッジ日時 2023-09-04 00:50:55
合計ジャッジ時間 6,614 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 18 ms
4,380 KB
testcase_03 AC 149 ms
11,188 KB
testcase_04 AC 108 ms
11,360 KB
testcase_05 AC 489 ms
16,940 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 70 ms
4,380 KB
testcase_08 AC 397 ms
16,900 KB
testcase_09 AC 286 ms
11,100 KB
testcase_10 AC 20 ms
4,376 KB
testcase_11 AC 55 ms
4,380 KB
testcase_12 AC 154 ms
11,044 KB
testcase_13 AC 155 ms
11,108 KB
testcase_14 AC 520 ms
16,944 KB
testcase_15 AC 287 ms
16,804 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 500 ms
16,828 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 303 ms
16,884 KB
testcase_20 AC 653 ms
16,900 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 656 ms
16,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }


enum MO = 10L^^9 + 7;

int N, K;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      
      auto dp2 = new long[][](K + 2, K + 2);
      dp2[0][1] = 1;
      foreach (k; 0 .. K) {
        auto dp2Next = new long[][](K + 2, K + 2);
        foreach (x; 0 .. K + 2) foreach (y; x .. K + 2) {
          long lot = N;
          if (x == y) {
            if ((K + 1) - x > 0) {
              --lot;
              (dp2Next[x + 1][y + 1] += dp2[x][y]) %= MO;
            }
          } else {
            if ((K + 1) - x > 0) {
              --lot;
              (dp2Next[x + 1][y] += dp2[x][y]) %= MO;
            }
            if ((K + 1) - y > 0) {
              --lot;
              (dp2Next[x][y + 1] += dp2[x][y]) %= MO;
            }
          }
          (dp2Next[x][y] += dp2[x][y] * lot) %= MO;
        }
        dp2 = dp2Next;
      }
      debug {
        writeln("dp2 = ", dp2);
      }
      auto sum2 = new long[K + 2];
      foreach (x; 0 .. K + 2) foreach (y; 0 .. K + 2) {
        if (x < y) {
          (sum2[x] += dp2[x][y]) %= MO;
        }
      }
      
      long ans;
      foreach (i; 1 .. N + 1) {
        if (i > K + 1) {
          foreach (x; 0 .. K + 2) {
            ans += sum2[x] * (i - x);
            ans %= MO;
          }
        } else {
          auto dp1 = new long[K + 2];
          dp1[0] = 1;
          foreach (k; 0 .. K) {
            auto dp1Next = new long[K + 2];
            foreach (x; 0 .. K + 2) {
              long lot = N;
              if (i - x > 0) {
                --lot;
                (dp1Next[x + 1] += dp1[x]) %= MO;
              }
              (dp1Next[x] += dp1[x] * lot) %= MO;
            }
            dp1 = dp1Next;
          }
          debug {
            writeln("i = ", i, ": dp1 = ", dp1);
          }
          foreach (x; 0 .. i) {
            ans += (dp1[x] - dp2[x][x]) * (i - x);
            ans %= MO;
          }
        }
      }
      ans = (ans % MO + MO) % MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0