結果

問題 No.2351 Butterfly in Summer
ユーザー ks2mks2m
提出日時 2023-06-16 21:28:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 900 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 74,372 KB
実行使用メモリ 56,392 KB
最終ジャッジ日時 2023-09-06 18:40:37
合計ジャッジ時間 5,947 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,360 KB
testcase_01 AC 121 ms
56,156 KB
testcase_02 AC 119 ms
55,892 KB
testcase_03 AC 121 ms
56,264 KB
testcase_04 AC 119 ms
56,080 KB
testcase_05 AC 118 ms
55,976 KB
testcase_06 AC 121 ms
55,804 KB
testcase_07 AC 120 ms
55,720 KB
testcase_08 AC 120 ms
55,716 KB
testcase_09 AC 120 ms
56,128 KB
testcase_10 AC 119 ms
55,632 KB
testcase_11 AC 121 ms
55,756 KB
testcase_12 AC 120 ms
55,628 KB
testcase_13 AC 116 ms
55,792 KB
testcase_14 AC 121 ms
55,920 KB
testcase_15 AC 121 ms
55,972 KB
testcase_16 AC 122 ms
55,796 KB
testcase_17 AC 122 ms
56,392 KB
testcase_18 AC 122 ms
55,732 KB
testcase_19 AC 121 ms
55,856 KB
testcase_20 AC 125 ms
56,028 KB
testcase_21 AC 123 ms
55,740 KB
testcase_22 AC 121 ms
56,384 KB
testcase_23 AC 120 ms
55,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		sc.close();

		int mod = 998244353;
		long v1 = (long) k * (k - 1);
		long v2 = v1 * n % mod;
		long u1 = power(k, n, mod);
		long u2 = modinv(u1, mod);
		long ans = v2 * u2 % mod;
		System.out.println(ans);
	}

	static long modinv(long a, int m) {
		long b = m;
		long u = 1;
		long v = 0;
		long tmp = 0;

		while (b > 0) {
			long t = a / b;
			a -= t * b;
			tmp = a;
			a = b;
			b = tmp;

			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}

		u %= m;
		if (u < 0) u += m;
		return u;
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			x %= m;
			val = val * x % m;
		}
		return val;
	}
}
0