結果

問題 No.623 fudan no modulus to tigau
ユーザー pekempeypekempey
提出日時 2017-12-23 00:32:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 70,180 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 15:08:55
合計ジャッジ時間 1,421 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

const int mod = 998244353;

struct Modint {
  int n;
  Modint(int n = 0) : n(n) {}
};

Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % mod); }
Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
Modint &operator*=(Modint &a, Modint b) { return a = a * b; }

int t[51], a[51], b[51];
Modint memo[51];

Modint f(int n, Modint x) {
  if (n == 0) return 1;
  if (n == 1) return x;
  if (memo[n].n != -1) return memo[n];

  if (t[n] == 1) return memo[n] = f(a[n], x) + f(b[n], x);
  if (t[n] == 2) return memo[n] = a[n] * f(b[n], x);
  return memo[n] = f(a[n], x) * f(b[n], x);
}

int main() {
  int n;
  cin >> n;

  for (int i = 2; i <= n; i++) {
    cin >> t[i] >> a[i] >> b[i];
  }

  int q;
  cin >> q;

  while (q--) {
    memset(memo, -1, sizeof(memo));
    int x;
    cin >> x;
    cout << f(n, x).n << endl;
  }
}
0