結果

問題 No.1006 Share an Integer
ユーザー ks2mks2m
提出日時 2020-03-06 23:19:39
言語 Java19
(openjdk 21)
結果
AC  
実行時間 851 ms / 2,000 ms
コード長 2,014 bytes
コンパイル時間 3,420 ms
コンパイル使用メモリ 78,004 KB
実行使用メモリ 68,272 KB
最終ジャッジ日時 2023-08-04 12:57:41
合計ジャッジ時間 14,114 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
57,052 KB
testcase_01 AC 177 ms
57,468 KB
testcase_02 AC 176 ms
56,876 KB
testcase_03 AC 159 ms
58,416 KB
testcase_04 AC 179 ms
57,020 KB
testcase_05 AC 166 ms
56,976 KB
testcase_06 AC 197 ms
57,072 KB
testcase_07 AC 181 ms
57,296 KB
testcase_08 AC 186 ms
57,004 KB
testcase_09 AC 207 ms
57,112 KB
testcase_10 AC 209 ms
56,988 KB
testcase_11 AC 666 ms
65,824 KB
testcase_12 AC 791 ms
68,008 KB
testcase_13 AC 828 ms
68,132 KB
testcase_14 AC 851 ms
68,016 KB
testcase_15 AC 776 ms
68,120 KB
testcase_16 AC 600 ms
63,932 KB
testcase_17 AC 689 ms
65,644 KB
testcase_18 AC 760 ms
67,664 KB
testcase_19 AC 744 ms
68,144 KB
testcase_20 AC 783 ms
67,804 KB
testcase_21 AC 842 ms
68,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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 x = sc.nextInt();
		sc.close();

		int x2 = x / 2;
		Eratosthenes er = new Eratosthenes(x);
		int min = Integer.MAX_VALUE;
		List<Integer> list = new ArrayList<>();
		for (int i = 1; i <= x2; i++) {
			int f1 = f(er, i);
			int f2 = f(er, x - i);
			int v = Math.abs(f1 - f2);
			if (v < min) {
				min = v;
				list.clear();
				list.add(i);
			} else if (v == min) {
				list.add(i);
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < list.size(); i++) {
			pw.println(list.get(i) + " " + (x - list.get(i)));
		}
		int start = list.size() - 1;
		if (x2 * 2 == x && list.get(start) == x2) {
			start--;
		}
		for (int i = start; i >= 0; i--) {
			pw.println(x - list.get(i) + " " + list.get(i));
		}
		pw.flush();
	}

	static int f(Eratosthenes er, int n) {
		if (n == 1) {
			return 0;
		}
		Map<Integer, Integer> soinsu = er.bunkai(n);
		int d = 1;
		for (int i : soinsu.values()) {
			d *= i + 1;
		}
		return n - d;
	}

	static class Eratosthenes {
		int[] div;

		public Eratosthenes(int n) {
			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];
				if (soinsu.containsKey(d)) {
					soinsu.put(d, soinsu.get(d) + 1);
				} else {
					soinsu.put(d, 1);
				}
				x /= d;
			}
			return soinsu;
		}

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