結果

問題 No.2668 Trees on Graph Paper
ユーザー 👑 emthrmemthrm
提出日時 2024-03-08 22:36:16
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,971 ms / 3,000 ms
コード長 1,918 bytes
コンパイル時間 4,571 ms
コンパイル使用メモリ 251,364 KB
実行使用メモリ 22,844 KB
最終ジャッジ日時 2024-03-08 22:36:54
合計ジャッジ時間 35,976 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 255 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 1,763 ms
15,016 KB
testcase_15 AC 2,718 ms
21,168 KB
testcase_16 AC 1,362 ms
12,308 KB
testcase_17 AC 133 ms
6,548 KB
testcase_18 AC 2,559 ms
20,244 KB
testcase_19 AC 2,213 ms
17,892 KB
testcase_20 AC 2,398 ms
19,116 KB
testcase_21 AC 2,262 ms
18,416 KB
testcase_22 AC 2,325 ms
18,732 KB
testcase_23 AC 1,498 ms
13,068 KB
testcase_24 AC 2,941 ms
22,844 KB
testcase_25 AC 2,941 ms
22,844 KB
testcase_26 AC 2,971 ms
22,844 KB
testcase_27 AC 2,942 ms
22,844 KB
testcase_28 AC 2 ms
6,548 KB
testcase_29 AC 2 ms
6,548 KB
testcase_30 AC 2 ms
6,548 KB
testcase_31 AC 2 ms
6,548 KB
testcase_32 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

#include <atcoder/modint>

int main() {
  int n, m; cin >> n >> m;
  atcoder::modint::set_mod(m);
  vector<atcoder::modint> dp1(n + 1, 0);
  dp1[1] = dp1[2] = 1;
  array<atcoder::modint, 9> dp2{}, nxt{};
  FOR(a, 1, 3) REP(b, a + 1) ++dp2[a * 3 + b];
  FOR(i, 3, n + 1) {
    FOR(x, 1, 3) for (int y = 0; y <= x && y <= 2; ++y) {
      const int id = x * 3 + y;
      if (dp2[id] == 0) continue;
      for (int a = 0; a <= 1 && a <= y; ++a) {
        dp1[i] += dp1[i - 1] * dp2[id] * (x == 1 || y == 1 || a == 1 ? 1 : i * 2 - 4);
      }
    }
    fill(nxt.begin(), nxt.end(), 0);
    FOR(x, 3, 5) for (int y = 2; y <= x && y <= 4; ++y) {
      const int id = (x - 2) * 3 + (y - 2);
      if (dp2[id] == 0) continue;
      for (int a = 1; a <= y && a <= 3; ++a) for (int b = 0; b <= a && b <= 2; ++b) {
        nxt[min(a, 2) * 3 + b] += dp2[id] * (x == 3 || y == 3 || a == 3 ? 1 : i * 2 - 4) * (y == 2 || a == 2 || b == 2 ? 1 : i * 2 - 3);
      }
    }
    dp2.swap(nxt);
  }
  atcoder::modint ans = dp1[n];
  FOR(i, 1, n * 2) ans *= i;
  cout << ans.val() << '\n';
  return 0;
}
0