結果

問題 No.1006 Share an Integer
ユーザー ks2mks2m
提出日時 2020-03-06 23:16:07
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,931 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 56,640 KB
最終ジャッジ日時 2024-04-22 10:29:51
合計ジャッジ時間 13,672 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 189 ms
42,932 KB
testcase_01 AC 182 ms
43,164 KB
testcase_02 AC 187 ms
43,280 KB
testcase_03 WA -
testcase_04 AC 189 ms
43,408 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 186 ms
43,108 KB
testcase_09 AC 202 ms
43,812 KB
testcase_10 AC 198 ms
43,452 KB
testcase_11 AC 688 ms
53,808 KB
testcase_12 AC 774 ms
56,120 KB
testcase_13 AC 798 ms
56,640 KB
testcase_14 AC 826 ms
56,544 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 726 ms
55,404 KB
testcase_19 AC 719 ms
55,164 KB
testcase_20 AC 739 ms
55,448 KB
testcase_21 AC 807 ms
56,468 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)));
		}
		for (int i = list.size() - 1; 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