結果

問題 No.2529 Treasure Hunter
ユーザー ks2mks2m
提出日時 2023-11-03 23:35:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 1,393 bytes
コンパイル時間 2,672 ms
コンパイル使用メモリ 77,644 KB
実行使用メモリ 65,492 KB
最終ジャッジ日時 2023-11-03 23:35:34
合計ジャッジ時間 6,300 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,424 KB
testcase_01 AC 216 ms
65,492 KB
testcase_02 AC 145 ms
58,088 KB
testcase_03 AC 144 ms
59,660 KB
testcase_04 AC 148 ms
59,628 KB
testcase_05 AC 145 ms
59,628 KB
testcase_06 AC 147 ms
59,700 KB
testcase_07 AC 150 ms
59,588 KB
testcase_08 AC 150 ms
59,520 KB
testcase_09 AC 143 ms
57,548 KB
testcase_10 AC 149 ms
59,624 KB
testcase_11 AC 145 ms
59,624 KB
testcase_12 AC 107 ms
59,636 KB
testcase_13 AC 108 ms
59,656 KB
testcase_14 AC 108 ms
59,632 KB
testcase_15 AC 110 ms
59,648 KB
testcase_16 AC 106 ms
59,620 KB
testcase_17 AC 108 ms
59,648 KB
testcase_18 AC 108 ms
59,636 KB
testcase_19 AC 109 ms
59,636 KB
testcase_20 AC 109 ms
59,640 KB
testcase_21 AC 109 ms
59,640 KB
testcase_22 AC 109 ms
59,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		int mod = 998244353;
		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < t; z++) {
			String[] sa = br.readLine().split(" ");
			int n = Integer.parseInt(sa[0]);
			int m = Integer.parseInt(sa[1]);

			long[][] dp = new long[3][m];
			dp[0][0] = 1;
			dp[1][0] = n;
			if (n >= 4) {
				dp[2][0] = (long) (n - 3) * (n - 2) / 2 + (n - 3);
			}

			long n21 = Math.max(dp[2][0] - (n - 3), 0);
			long n22 = Math.max(dp[2][0] - (n - 3) * 2 + 1, 0);
			dp[2][0] %= mod;
			n21 %= mod;
			n22 %= mod;

			for (int i = 1; i < m; i++) {
				dp[0][i] += dp[0][i - 1] + dp[1][i - 1] + dp[2][i - 1];
				dp[0][i] %= mod;

				dp[1][i] += dp[0][i - 1] * n % mod;
				dp[1][i] += dp[1][i - 1] * (n - 1) % mod;
				dp[1][i] += dp[2][i - 1] * (n - 2) % mod;
				dp[1][i] %= mod;

				if (n >= 4) {
					dp[2][i] += dp[0][i - 1] * dp[2][0] % mod;
					dp[2][i] += dp[1][i - 1] * n21 % mod;
					dp[2][i] += dp[2][i - 1] * n22 % mod;
					dp[2][i] %= mod;
				}
			}
			long ans = dp[0][m - 1] + dp[1][m - 1] + dp[2][m - 1];
			ans %= mod;
			pw.println(ans);
		}
		pw.flush();
		br.close();
	}
}
0