結果

問題 No.623 fudan no modulus to tigau
ユーザー 0w10w1
提出日時 2017-12-26 16:03:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 767 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 202,528 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 21:57:24
合計ジャッジ時間 3,030 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

const int MOD = 998244353;

signed main() {
  std::ios::sync_with_stdio(false);
  int N;
  std::cin >> N;
  std::vector<int> T(N + 1), A(N + 1), B(N + 1);
  for (int i = 2; i <= N; ++i) {
    std::cin >> T[i] >> A[i] >> B[i];
  }
  int Q;
  std::cin >> Q;
  while (Q--) {
    int X;
    std::cin >> X;
    std::vector<int> dp(N + 1, -1);
    std::function<int(int, int)> f = [&](int n, int x) {
      if (n == 0) return 1;
      if (n == 1) return x;
      if (~dp[n]) return dp[n];
      if (T[n] == 1) return dp[n] = (f(A[n], x) + f(B[n], x)) % MOD;
      if (T[n] == 2) return dp[n] = 1LL * A[n] * f(B[n], x) % MOD;
      return dp[n] = 1LL * f(A[n], x) * f(B[n], x) % MOD;
    };
    std::cout << f(N, X) << std::endl;
  }
  return 0;
}
0