結果

問題 No.2668 Trees on Graph Paper
ユーザー MisukiMisuki
提出日時 2024-03-08 23:46:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,290 ms / 3,000 ms
コード長 4,564 bytes
コンパイル時間 2,557 ms
コンパイル使用メモリ 199,984 KB
実行使用メモリ 237,560 KB
最終ジャッジ日時 2024-03-08 23:47:34
合計ジャッジ時間 27,768 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 197 ms
23,636 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 1,366 ms
143,608 KB
testcase_15 AC 2,074 ms
217,408 KB
testcase_16 AC 1,061 ms
111,096 KB
testcase_17 AC 104 ms
13,808 KB
testcase_18 AC 1,961 ms
206,324 KB
testcase_19 AC 1,692 ms
178,140 KB
testcase_20 AC 1,837 ms
192,796 KB
testcase_21 AC 1,756 ms
184,400 KB
testcase_22 AC 1,787 ms
188,196 KB
testcase_23 AC 1,148 ms
120,224 KB
testcase_24 AC 2,290 ms
237,560 KB
testcase_25 AC 2,260 ms
237,560 KB
testcase_26 AC 2,288 ms
237,560 KB
testcase_27 AC 2,281 ms
237,560 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define clock chrono::steady_clock::now().time_since_epoch().count()

#ifdef DEBUG
#define dbg(x) cout << (#x) << " = " << x << '\n'
#else
#define dbg(x)
#endif

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(const T &x : s)
    os << x << ' ';
  return os;
}

//note: inversion only works when MOD is a prime

struct mint {
  static long long MOD;
  long long _val;

  mint(long long init = 0) {
    _val = init % MOD;
    (*this).norm();
  }

  mint POW(long long index) {
    if (index == 0)
      return mint(1ll);
    mint base = *this;
    mint res = (base == 0ll ? 0ll : 1ll);
    while(index) {
      if (index & 1)
        res *= base;
      base *= base, index >>= 1;
    }
    return res;
  }

  mint inv() { return (*this).POW(MOD - 2); }

  mint& norm() {
    if (_val >= MOD)
      _val -= MOD;
    if (_val < 0)
      _val += MOD;
    return *this;
  }

  mint& operator+=(mint b) {
    _val += b._val;
    return (*this).norm();
  }
  mint& operator-=(mint b) {
    _val -= b._val;
    return (*this).norm();
  }
  mint& operator*=(mint b) {
    _val = (_val * b._val) % MOD;
    return *this;
  }
  mint& operator/=(mint b) {
    _val = (_val * b.inv()._val) % MOD;
    return *this;
  }

  mint& operator++() {
    _val += 1;
    return (*this).norm();
  }
  mint& operator--() {
    _val -= 1;
    return (*this).norm();
  }
  mint operator++(signed) {
    mint tmp = *this;
    ++(*this);
    return tmp;
  }
  mint operator--(signed) {
    mint tmp = *this;
    --(*this);
    return tmp;
  }

  mint operator-() { return mint(-_val); }
  bool operator==(mint b) { return _val == b._val; }
  bool operator!=(mint b) { return _val != b._val; }
  
  friend mint operator+(mint a, mint b) { return a += b; }
  friend mint operator-(mint a, mint b) { return a -= b; }
  friend mint operator*(mint a, mint b) { return a *= b; }
  friend mint operator/(mint a, mint b) { return a /= b; }

  friend ostream& operator<<(ostream& os, const mint& b) {
    return os << b._val;
  }
  friend istream& operator>>(istream& is, mint& b) {
    long long val;
    is >> val;
    b = mint(val);
    return is;
  }
};

ll mint::MOD = 2;

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n; cin >> n >> mint::MOD;
  vector<array<mint, 3>> dp(2 * n);
  dp[1] = {1, 1, 1};
  for(int i = 1; i < 2 * n - 3; i++) {
    for(int j : {0, 1, 2}) {
      dp[i + 1][j] += dp[i][0];
      dp[i + 2][j] += dp[i][1] * (i + 1);
      dp[i + 3][j] += dp[i][2] * (i + 1) * (i + 2);
    }
    for(int j : {1, 2}) {
      dp[i + 1][j] += dp[i][1];
      dp[i + 2][j] += dp[i][2] * (i + 1);
    }
    dp[i + 1][2] += dp[i][2];
  }

  mint ans = 1;
  for(int i = 2; i <= (2 * n - 1); i++) ans *= i;
  for(int i = 1; i <= 2 * n - 3; i += 2) ans *= dp[i][2];
  cout << ans << '\n';

  return 0;
}
0