結果

問題 No.1396 Giri
ユーザー ks2mks2m
提出日時 2021-02-14 21:34:28
言語 Java19
(openjdk 21)
結果
AC  
実行時間 785 ms / 2,000 ms
コード長 1,973 bytes
コンパイル時間 2,701 ms
コンパイル使用メモリ 81,784 KB
実行使用メモリ 68,852 KB
最終ジャッジ日時 2023-09-29 14:46:05
合計ジャッジ時間 12,572 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,048 KB
testcase_01 AC 120 ms
55,356 KB
testcase_02 AC 778 ms
67,284 KB
testcase_03 AC 120 ms
55,652 KB
testcase_04 AC 120 ms
55,604 KB
testcase_05 AC 753 ms
68,704 KB
testcase_06 AC 124 ms
55,636 KB
testcase_07 AC 123 ms
55,700 KB
testcase_08 AC 123 ms
55,704 KB
testcase_09 AC 122 ms
56,096 KB
testcase_10 AC 122 ms
55,728 KB
testcase_11 AC 120 ms
55,620 KB
testcase_12 AC 119 ms
56,152 KB
testcase_13 AC 121 ms
56,212 KB
testcase_14 AC 122 ms
55,652 KB
testcase_15 AC 124 ms
55,784 KB
testcase_16 AC 159 ms
56,128 KB
testcase_17 AC 178 ms
58,652 KB
testcase_18 AC 285 ms
59,576 KB
testcase_19 AC 500 ms
65,184 KB
testcase_20 AC 597 ms
67,260 KB
testcase_21 AC 692 ms
67,240 KB
testcase_22 AC 727 ms
67,500 KB
testcase_23 AC 785 ms
68,692 KB
testcase_24 AC 743 ms
68,564 KB
testcase_25 AC 783 ms
68,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
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();
		sc.close();

		int mod = 998244353;
		Eratosthenes era = new Eratosthenes(n);
		Map<Integer, Integer> map = new HashMap<>();
		for (int i = 2; i <= n; i++) {
			Map<Integer, Integer> res = era.bunkai(i);
			for (Integer k : res.keySet()) {
				map.put(k, Math.max(map.getOrDefault(k, 0), res.get(k)));
			}
		}

		long ans = 1;
		int max = 1;
		for (Integer k : map.keySet()) {
			max = Math.max(max, k);
			ans *= power(k, map.get(k), mod);
			ans %= mod;
		}
		ans *= modinv(max, mod);
		ans %= 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) {
			val = val * x % m;
		}
		return val;
	}

	static class Eratosthenes {
		int[] div;

		public Eratosthenes(int n) {
			if (n < 2) return;
			div = new int[n + 1];
			div[0] = -1;
			div[1] = -1;
			int end = (int) Math.sqrt(n) + 1;
			for (int i = 2; i <= end; i++) {
				if (div[i] == 0) {
					div[i] = i;
					for (int j = i * i; j <= n; j+=i) {
						if (div[j] == 0) div[j] = i;
					}
				}
			}
			for (int i = end + 1; i <= n; i++) {
				if (div[i] == 0) div[i] = i;
			}
		}

		public Map<Integer, Integer> bunkai(int x) {
			Map<Integer, Integer> soinsu = new HashMap<>();
			while (x > 1) {
				Integer d = div[x];
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				x /= d;
			}
			return soinsu;
		}

		public boolean isSosuu(int x) {
			return div[x] == x;
		}
	}
}
0