結果

問題 No.811 約数の個数の最大化
ユーザー htensaihtensai
提出日時 2020-01-26 09:25:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 2,022 bytes
コンパイル時間 3,098 ms
コンパイル使用メモリ 75,380 KB
実行使用メモリ 59,620 KB
最終ジャッジ日時 2023-10-12 11:56:19
合計ジャッジ時間 6,960 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,916 KB
testcase_01 AC 128 ms
55,884 KB
testcase_02 AC 224 ms
59,620 KB
testcase_03 AC 125 ms
55,856 KB
testcase_04 AC 124 ms
55,704 KB
testcase_05 AC 134 ms
55,864 KB
testcase_06 AC 150 ms
55,664 KB
testcase_07 AC 155 ms
55,684 KB
testcase_08 AC 205 ms
58,908 KB
testcase_09 AC 194 ms
58,900 KB
testcase_10 AC 191 ms
58,576 KB
testcase_11 AC 202 ms
59,020 KB
testcase_12 AC 189 ms
56,152 KB
testcase_13 AC 203 ms
58,728 KB
testcase_14 AC 199 ms
59,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        int x = n;
        for (int i = 2; i <= Math.sqrt(x); i++) {
            while (x % i == 0) {
                if (map.containsKey(i)) {
                    map.put(i, map.get(i) + 1);
                } else {
                    map.put(i, 1);
                }
                x /= i;
            }
        }
        if (x > 1) {
            if (map.containsKey(x)) {
                map.put(x, map.get(x) + 1);
            } else {
                map.put(x, 1);
            }
        }
        int max = 0;
        int maxNumber = 0;
        for (int i = 2; i < n; i++) {
            int y = i;
            int count = 0;
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                for (int j = 0; j < entry.getValue() && count < k; j++) {
                    if (y % entry.getKey() == 0) {
                        y /= entry.getKey();
                        count++;
                    } else {
                        break;
                    }
                }
                if (count >= k) {
                    break;
                }
            }
            if (count >= k) {
                int m = getCount(i);
                if (max < m) {
                    max = m;
                    maxNumber = i;
                }
            }
        }
        System.out.println(maxNumber);
    }
    
    static int getCount(int x) {
        int count = 1;
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                int cnt = 0;
                while(x % i == 0) {
                    cnt++;
                    x /= i;
                }
                count *= (cnt + 1);
            } 
        }
        if (x > 1) {
            count *= 2;
        }
        return count;
    }

}
0