結果

問題 No.1006 Share an Integer
ユーザー tentententen
提出日時 2020-08-21 20:35:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 1,598 bytes
コンパイル時間 2,430 ms
コンパイル使用メモリ 79,472 KB
実行使用メモリ 60,444 KB
最終ジャッジ日時 2024-04-23 05:17:22
合計ジャッジ時間 6,244 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
54,252 KB
testcase_01 AC 104 ms
52,992 KB
testcase_02 AC 111 ms
54,340 KB
testcase_03 AC 110 ms
54,340 KB
testcase_04 AC 112 ms
54,428 KB
testcase_05 AC 104 ms
53,040 KB
testcase_06 AC 109 ms
53,144 KB
testcase_07 AC 115 ms
54,052 KB
testcase_08 AC 105 ms
53,068 KB
testcase_09 AC 112 ms
54,056 KB
testcase_10 AC 106 ms
53,376 KB
testcase_11 AC 171 ms
60,444 KB
testcase_12 AC 214 ms
52,104 KB
testcase_13 AC 224 ms
53,328 KB
testcase_14 AC 227 ms
53,228 KB
testcase_15 AC 205 ms
51,408 KB
testcase_16 AC 164 ms
48,864 KB
testcase_17 AC 170 ms
49,052 KB
testcase_18 AC 209 ms
51,776 KB
testcase_19 AC 196 ms
51,352 KB
testcase_20 AC 203 ms
51,832 KB
testcase_21 AC 229 ms
53,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int[] counts = new int[x + 1];
		for (int i = 1; i <= x; i++) {
		    counts[i] = i - 1;
		}
		for (int i = 2; i <= x; i++) {
		    for (int j = 1; j * i <= x; j++) {
		        counts[j * i]--;
		    }
		}
		int min = Integer.MAX_VALUE;
		Answer.size = x;
		ArrayList<Answer> list = new ArrayList<>();
		for (int i = 1; i <= x / 2; i++) {
		    if (min >= Math.abs(counts[i] - counts[x - i])) {
		        if (min > Math.abs(counts[i] - counts[x - i])) {
		            list.clear();
		            min = Math.abs(counts[i] - counts[x - i]);
		        }
		        list.add(new Answer(i));
		        if (i * 2 < x) {
		            list.add(new Answer(x - i));
		        }
		    }
		}
		Collections.sort(list);
		StringBuilder sb = new StringBuilder();
		for (Answer a : list) {
		    sb.append(a).append("\n");
		}
		System.out.print(sb);
    }
    

    static class Answer implements Comparable<Answer> {
        static int size;
        int left;
        int right;
        
        public Answer(int left, int right) {
            this.left = left;
            this.right = right;
        }
        
        
        public Answer(int left) {
            this(left, size - left);
        }
        
        public int compareTo(Answer another) {
            return left - another.left;
        }
        
        public String toString() {
            return new StringBuilder().append(left).append(" ").append(right).toString();
        }
    }
}

0