結果

問題 No.824 Many Shifts Hard
ユーザー 👑 emthrmemthrm
提出日時 2019-10-06 04:20:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,445 ms / 2,000 ms
コード長 6,703 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 126,044 KB
実行使用メモリ 223,488 KB
最終ジャッジ日時 2024-04-17 00:10:58
合計ジャッジ時間 13,242 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 37 ms
12,032 KB
testcase_03 AC 307 ms
63,360 KB
testcase_04 AC 283 ms
36,224 KB
testcase_05 AC 1,026 ms
168,064 KB
testcase_06 AC 19 ms
5,376 KB
testcase_07 AC 234 ms
16,384 KB
testcase_08 AC 764 ms
151,936 KB
testcase_09 AC 740 ms
95,360 KB
testcase_10 AC 60 ms
6,016 KB
testcase_11 AC 182 ms
13,952 KB
testcase_12 AC 370 ms
53,120 KB
testcase_13 AC 500 ms
132,736 KB
testcase_14 AC 904 ms
223,360 KB
testcase_15 AC 756 ms
196,864 KB
testcase_16 AC 46 ms
14,720 KB
testcase_17 AC 883 ms
221,824 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 856 ms
223,488 KB
testcase_20 AC 1,432 ms
223,488 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 1,445 ms
223,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int mod = MOD;
struct ModInt {
  unsigned val;
  ModInt(): val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {}
  ModInt pow(long long exponent) {
    ModInt tmp = *this, res = 1;
    while (exponent > 0) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
      exponent >>= 1;
    }
    return res;
  }
  ModInt &operator+=(const ModInt &rhs) { if((val += rhs.val) >= mod) val -= mod; return *this; }
  ModInt &operator-=(const ModInt &rhs) { if((val += mod - rhs.val) >= mod) val -= mod; return *this; }
  ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; }
  ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); }
  bool operator==(const ModInt &rhs) const { return val == rhs.val; }
  bool operator!=(const ModInt &rhs) const { return val != rhs.val; }
  bool operator<(const ModInt &rhs) const { return val < rhs.val; }
  bool operator<=(const ModInt &rhs) const { return val <= rhs.val; }
  bool operator>(const ModInt &rhs) const { return val > rhs.val; }
  bool operator>=(const ModInt &rhs) const { return val >= rhs.val; }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; }
  friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; }
  friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; }
private:
  ModInt inv() const {
    // if (__gcd(static_cast<int>(val), mod) != 1) assert(false);
    unsigned a = val, b = mod; int x = 1, y = 0;
    while (b) {
      unsigned tmp = a / b;
      swap(a -= tmp * b, b);
      swap(x -= tmp * y, y);
    }
    return ModInt(x);
  }
};
ModInt abs(const ModInt &x) { return x.val; }
struct Combinatorics {
  Combinatorics(int MAX = 5000000) {
    MAX <<= 1;
    fact.resize(MAX + 1);
    fact_inv.resize(MAX + 1);
    fact[0] = 1;
    FOR(i, 1, MAX + 1) fact[i] = fact[i - 1] * i;
    fact_inv[MAX] = ModInt(1) / fact[MAX];
    for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i;
  }
  ModInt nCk(int n, int k) {
    if (n < 0 || n < k || k < 0) return ModInt(0);
    return fact[n] * fact_inv[k] * fact_inv[n - k];
  }
  ModInt nPk(int n, int k) {
    if (n < 0 || n < k || k < 0) return ModInt(0);
    return fact[n] * fact_inv[n - k];
  }
  ModInt nHk(int n, int k) {
    if (n < 0 || k < 0) return ModInt(0);
    return (k == 0 ? ModInt(1) : nCk(n + k - 1, k));
  }
private:
  vector<ModInt> fact, fact_inv;
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  // 操作開始前の時点でマス i と i + 1 にある駒に注目する
  // マス i + 1 にあった駒をマス i にあった駒と合流することなく
  // マス j に移して操作を終えた場合,得点の総和は j 増加する

  int n, k; cin >> n >> k;

  // まずは操作中にマス 0 に到達する可能性のある駒に関わる場合の数を求める
  // dp1[tm][i][j] = 操作開始前,1 <= m + 1 <= min(n, k) を満たす
  //                 マス m と m + 1 に存在している駒に注目した場合
  //                 tm 回の操作を終えた時点でマス i と j に駒が存在する場合の数
  vector<vector<vector<ModInt> > > dp1(k + 1, vector<vector<ModInt> >(k + 1, vector<ModInt>(k + 1, 0)));
  // 初期値
  FOR(j, 1, k + 1) {
    if (j > n) break;
    dp1[0][j - 1][j] = 1;
  }
  REP(tm, k) {
    REP(i, k + 1) FOR(j, i + 1, k + 1) {
      // マス i に存在する駒をマス i - 1 に移す
      if (i >= 1) dp1[tm + 1][i - 1][j] += dp1[tm][i][j];
      // マス j に存在する駒をマス j - 1 に移す
      if (i < j - 1) dp1[tm + 1][i][j - 1] += dp1[tm][i][j];
      // マス i と j 以外に存在する駒を移す
      if (i == 0) {
        dp1[tm + 1][i][j] += dp1[tm][i][j] * (n - 1);
      } else {
        dp1[tm + 1][i][j] += dp1[tm][i][j] * (n - 2);
      }
    }
  }
  ModInt ans = 0;
  REP(i, k + 1) FOR(j, i + 1, k + 1) ans += dp1[k][i][j] * j;

  // 次に先ほど注目しなかった駒が総和に寄与する場合を考える
  // dp2[tm][i][j] = 操作開始前,k < m + 1 <= n を満たす
  //                 マス m と m + 1 に存在している駒の内,ある一組に注目した場合
  //                 tm 回の操作を終えた時点でマス m - (k - i) と m + 1 - (k + 1 - j) に駒が存在する場合の数
  vector<vector<vector<ModInt> > > dp2(k + 1, vector<vector<ModInt> >(k + 2, vector<ModInt>(k + 2, 0)));
  dp2[0][k][k + 1] = 1;
  REP(tm, k) {
    REP(i, k + 2) FOR(j, i + 1, k + 2) {
      // マス i に存在する駒をマス i - 1 に移す
      if (i >= 1) dp2[tm + 1][i - 1][j] += dp2[tm][i][j];
      // マス j に存在する駒をマス j - 1 に移す
      if (i < j - 1) dp2[tm + 1][i][j - 1] += dp2[tm][i][j];
      // マス i と j 以外に存在する駒を移す
      if (i == 0) {
        // この場合は存在しない
        dp2[tm + 1][i][j] += dp2[tm][i][j] * (n - 1);
      } else {
        dp2[tm + 1][i][j] += dp2[tm][i][j] * (n - 2);
      }
    }
  }
  FOR(j, 1, k + 2) {
    ModInt tmp = 0; // マス m + 1 に存在していた駒がマス j に移る場合の数
    REP(i, j) tmp += dp2[k][i][j];
    FOR(x, k + 1, n + 1) { // m + 1 = x
      int score = x - (k + 1 - j); // 得点
      ans += tmp * score;
    }
  }

  cout << ans << '\n';
  return 0;
}
0