結果

問題 No.1006 Share an Integer
ユーザー ks2mks2m
提出日時 2020-03-06 23:19:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 831 ms / 2,000 ms
コード長 2,014 bytes
コンパイル時間 3,016 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 56,572 KB
最終ジャッジ日時 2024-04-22 10:34:57
合計ジャッジ時間 14,213 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 184 ms
42,840 KB
testcase_01 AC 188 ms
43,272 KB
testcase_02 AC 186 ms
43,448 KB
testcase_03 AC 153 ms
42,316 KB
testcase_04 AC 191 ms
43,332 KB
testcase_05 AC 175 ms
42,800 KB
testcase_06 AC 192 ms
43,424 KB
testcase_07 AC 190 ms
42,884 KB
testcase_08 AC 196 ms
43,592 KB
testcase_09 AC 206 ms
43,420 KB
testcase_10 AC 190 ms
43,228 KB
testcase_11 AC 680 ms
53,388 KB
testcase_12 AC 780 ms
55,972 KB
testcase_13 AC 806 ms
56,512 KB
testcase_14 AC 831 ms
56,572 KB
testcase_15 AC 753 ms
55,100 KB
testcase_16 AC 593 ms
52,264 KB
testcase_17 AC 653 ms
52,948 KB
testcase_18 AC 751 ms
55,296 KB
testcase_19 AC 723 ms
55,152 KB
testcase_20 AC 763 ms
55,640 KB
testcase_21 AC 804 ms
56,536 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