結果

問題 No.824 Many Shifts Hard
ユーザー pekempeypekempey
提出日時 2019-06-30 17:06:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,606 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 166,832 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-20 11:13:07
合計ジャッジ時間 3,205 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,504 KB
testcase_02 AC 13 ms
4,376 KB
testcase_03 AC 35 ms
4,380 KB
testcase_04 AC 24 ms
4,376 KB
testcase_05 AC 70 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 16 ms
4,376 KB
testcase_08 AC 65 ms
4,376 KB
testcase_09 AC 47 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 14 ms
4,380 KB
testcase_12 AC 31 ms
4,376 KB
testcase_13 AC 59 ms
4,376 KB
testcase_14 AC 88 ms
4,380 KB
testcase_15 AC 79 ms
4,380 KB
testcase_16 AC 15 ms
4,376 KB
testcase_17 AC 88 ms
4,416 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 88 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 88 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

const int MOD = 1000000007;

struct mint { int n; mint(int n_ = 0) : n(n_) {} };
mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
istream &operator>>(istream &i, mint &a) { return i >> a.n; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }

mint dp0[305][305];
mint dp1[305][305];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  if (N == 1) {
    cout << 0 << '\n';
    return 0;
  }
  for (int i = 0; i <= K-1; i++) {
    dp0[i][i+1]=1;
  }
  rep(i, K) {
    memset(dp1, 0, sizeof(dp1));
    rep(j, K+1) rep(k, K+1) {
      if (dp0[j][k].n == 0) continue;
      dp1[max(0,j-1)][k] += dp0[j][k];
      if (j < k-1) dp1[j][k-1] += dp0[j][k];
      dp1[j][k] += dp0[j][k]*(N-2);
    }
    swap(dp0, dp1);
  }
  mint ans = 0;
  for (int i = 1; i <= N; i++) {
    if (i + K <= N) {
      ans += i * dp0[0][1];
    } else {
      int x = N - i + 1;
      for (int j = 0; j < K-x+1; j++) {
        ans += i * dp0[j][K-x+1];
      }
    }
  }
  cout << ans << '\n';
}
0