結果

問題 No.1529 Constant Lcm
ユーザー ks2mks2m
提出日時 2021-06-04 20:28:33
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,551 ms / 3,000 ms
コード長 1,779 bytes
コンパイル時間 5,076 ms
コンパイル使用メモリ 76,500 KB
実行使用メモリ 76,020 KB
最終ジャッジ日時 2023-08-12 09:32:44
合計ジャッジ時間 24,632 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,768 KB
testcase_01 AC 159 ms
56,184 KB
testcase_02 AC 126 ms
56,384 KB
testcase_03 AC 123 ms
56,204 KB
testcase_04 AC 127 ms
55,780 KB
testcase_05 AC 127 ms
56,056 KB
testcase_06 AC 124 ms
56,048 KB
testcase_07 AC 124 ms
56,124 KB
testcase_08 AC 123 ms
55,620 KB
testcase_09 AC 124 ms
56,128 KB
testcase_10 AC 1,306 ms
70,364 KB
testcase_11 AC 753 ms
66,896 KB
testcase_12 AC 244 ms
59,140 KB
testcase_13 AC 1,195 ms
70,180 KB
testcase_14 AC 898 ms
68,868 KB
testcase_15 AC 986 ms
68,872 KB
testcase_16 AC 746 ms
67,488 KB
testcase_17 AC 554 ms
65,816 KB
testcase_18 AC 662 ms
66,668 KB
testcase_19 AC 889 ms
68,124 KB
testcase_20 AC 1,517 ms
75,612 KB
testcase_21 AC 1,539 ms
76,020 KB
testcase_22 AC 1,536 ms
74,248 KB
testcase_23 AC 1,512 ms
73,472 KB
testcase_24 AC 1,500 ms
73,012 KB
testcase_25 AC 1,551 ms
74,496 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();

		Eratosthenes era = new Eratosthenes(n);
		Map<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i < n; i++) {
			Map<Integer, Integer> map1 = era.bunkai(i);
			Map<Integer, Integer> map2 = era.bunkai(n - i);
			for (Integer key : map2.keySet()) {
				map1.put(key, map1.getOrDefault(key, 0) + map2.get(key));
			}
			for (Integer key : map1.keySet()) {
				map.put(key, Math.max(map.getOrDefault(key, 0), map1.get(key)));
			}
		}

		int mod = 998244353;
		long ans = 1;
		for (Integer key : map.keySet()) {
			ans *= power(key, map.get(key), mod);
			ans %= mod;
		}
		System.out.println(ans);
	}

	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