結果

問題 No.1006 Share an Integer
ユーザー tenten
提出日時 2020-08-21 18:16:30
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,950 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 79,432 KB
実行使用メモリ 65,552 KB
最終ジャッジ日時 2024-10-15 04:53:40
合計ジャッジ時間 7,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8 TLE * 1 -- * 10
権限があれば一括ダウンロードができます

ソースコード

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 - getCount(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 int getCount(int x) {
        if (x == 1) {
            return 1;
        } else {
            int count = 2;
            for (int i = 2; i <= Math.sqrt(x); i++) {
                if (x % i != 0) {
                    continue;
                }
                if (i * i == x) {
                    count++;
                } else {
                    count += 2;
                }
            }
            return count;
        }
    }
    
    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