提出 #25619839


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, n) for (int i = a; i < (int)n; i++)
#define REP(i, n) FOR(i, 0, n)
#define FOREQ(i, a, n) FOR(i, a, n+1)


struct Mod {
  using lli = long long int;

  static constexpr lli M = 998244353;
  lli n;

  Mod(): n(0) {}
  Mod(long long int m): n(m) {
    if (m > M)
      n = m % M;
    else if (m < 0)
      n = (m % M + M) % M;
  }
  explicit operator lli() const { return n; }

  Mod& operator+=(const Mod& a) {
    n = (n + a.n) % M;
    return *this;
  }
  Mod& operator-=(const Mod& a) {
    n = (n + M - a.n) % M;
    return *this;
  }
  Mod& operator*=(const Mod& a) {
    n = (n * a.n) % M;
    return *this;
  }
  Mod operator/=(const Mod &a) {
    return *this *= Mod::inv(lli(a), M);
  }
  Mod operator+(const Mod& a) const {
    Mod res = *this;
    return res += a;
  }
  Mod operator-(const Mod& a) const {
    Mod res = *this;
    return res -= a;
  }
  Mod operator*(const Mod& a) const {
    Mod res = *this;
    return res *= a;
  }
  Mod operator/(const Mod &a) const {
    return *this * Mod::inv(lli(a), M);
  }
  Mod operator^(lli m) const {
    if (m == 0) return Mod(1);
    const Mod a = *this;
    Mod res = (a * a) ^ (m / 2);
    return m % 2 ? res * a : res;
  }
  static lli inv(lli a, lli p) {
    return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
  }
  static Mod fact(int n, bool sw = true) {
    static std::vector<Mod> v1 = { 1 }, v2 = { 1 };
    if (n >= (int)v1.size()) {
      const int from = v1.size(), to = n + 1024;
      v1.reserve(to);
      v2.reserve(to);
      for (int i = from; i < to; ++i) {
        v1.push_back(v1.back() * Mod(i));
        v2.push_back(v2.back() / Mod(i));
      }
    }
    return sw ? v1[n] : v2[n];
  }
  static Mod comb(int a, int b) {
    if (b == 0) return 1;
    if (b < 0 || b > a) return 0;
    return Mod::fact(a, true) * Mod::fact(b, false) *
           Mod::fact(a - b, false);
  }
};

istream& operator>>(istream& is, Mod& m) { return is >> m.n; }
ostream& operator<<(ostream& os, const Mod& m) { return os << m.n; }


int  N, M, A, B;
bool good[401][401];
Mod  memo[401][401];
bool used[401][401];

Mod loop(int first, int last) {
  if (used[first][last]) { return memo[first][last]; }

  used[first][last] = true;
  if (first == last) { return memo[first][last] = Mod(1); }

  Mod res(0);
  for (int mid = first + 2; mid <= last-2; mid += 2) {
    if (!good[first][mid-1]) { continue; }
    res += Mod::comb((last-first)/2, (mid-first)/2) * loop(first+1, mid-1) * loop(mid, last);
  }
  res += good[first][last-1] ? loop(first+1, last-1) : Mod(0);
  return memo[first][last] = res;
}

int main() {
  cin >> N >> M;
  REP(i, 401) REP(j, 401) { good[i][j] = used[i][j] = false; }
  REP(i, M) { cin >> A >> B; good[A-1][B-1] = good[B-1][A-1] = true; }
  cout << loop(0, 2*N) << endl;

  return 0;
}

提出情報

提出日時
問題 F - Make Pair
ユーザ ryo_ryo66
言語 C++ (GCC 9.2.1)
得点 500
コード長 2958 Byte
結果 AC
実行時間 97 ms
メモリ 5240 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 28
セット名 テストケース
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 9 ms 5072 KiB
example_01.txt AC 4 ms 5136 KiB
example_02.txt AC 4 ms 5168 KiB
hand_00.txt AC 97 ms 5076 KiB
hand_01.txt AC 80 ms 5240 KiB
hand_02.txt AC 4 ms 4980 KiB
hand_03.txt AC 4 ms 5020 KiB
hand_04.txt AC 6 ms 5228 KiB
hand_05.txt AC 5 ms 5036 KiB
hand_06.txt AC 4 ms 5164 KiB
random_00.txt AC 5 ms 5216 KiB
random_01.txt AC 11 ms 5012 KiB
random_02.txt AC 4 ms 5216 KiB
random_03.txt AC 76 ms 5236 KiB
random_04.txt AC 81 ms 5072 KiB
random_05.txt AC 9 ms 5080 KiB
random_06.txt AC 63 ms 5176 KiB
random_07.txt AC 74 ms 5204 KiB
random_08.txt AC 79 ms 5092 KiB
random_09.txt AC 49 ms 5084 KiB
random_10.txt AC 5 ms 5184 KiB
random_11.txt AC 56 ms 5196 KiB
random_12.txt AC 33 ms 5016 KiB
random_13.txt AC 38 ms 5080 KiB
random_14.txt AC 40 ms 5080 KiB
random_15.txt AC 5 ms 4980 KiB
random_16.txt AC 16 ms 5084 KiB
random_17.txt AC 18 ms 5056 KiB