結果

問題 No.2570 最大最大公約数
ユーザー tentententen
提出日時 2023-12-05 10:59:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,855 bytes
コンパイル時間 2,994 ms
コンパイル使用メモリ 92,324 KB
実行使用メモリ 61,268 KB
最終ジャッジ日時 2023-12-05 10:59:18
合計ジャッジ時間 8,684 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 179 ms
56,548 KB
testcase_01 AC 130 ms
55,908 KB
testcase_02 AC 186 ms
56,568 KB
testcase_03 AC 131 ms
56,540 KB
testcase_04 AC 232 ms
59,648 KB
testcase_05 AC 237 ms
61,200 KB
testcase_06 AC 226 ms
59,132 KB
testcase_07 AC 216 ms
60,668 KB
testcase_08 AC 219 ms
60,608 KB
testcase_09 AC 248 ms
59,524 KB
testcase_10 AC 213 ms
61,268 KB
testcase_11 AC 211 ms
61,188 KB
testcase_12 AC 222 ms
61,064 KB
testcase_13 AC 262 ms
60,152 KB
testcase_14 AC 192 ms
60,848 KB
testcase_15 AC 209 ms
58,736 KB
testcase_16 AC 191 ms
57,008 KB
testcase_17 AC 56 ms
54,072 KB
testcase_18 AC 78 ms
54,896 KB
testcase_19 AC 74 ms
54,500 KB
testcase_20 AC 58 ms
53,988 KB
testcase_21 AC 86 ms
55,504 KB
testcase_22 AC 59 ms
54,004 KB
testcase_23 AC 56 ms
54,004 KB
testcase_24 AC 83 ms
55,152 KB
testcase_25 AC 78 ms
55,140 KB
testcase_26 AC 60 ms
54,096 KB
testcase_27 AC 57 ms
52,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[] values = new long[n];
        HashSet<Long> type = new HashSet<>();
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextLong();
            for (long j = 1; j <= Math.sqrt(values[i]); j++) {
                if (values[i] % j == 0) {
                    type.add(j);
                    type.add(values[i] / j);
                }
            }
        }
        long ans = 1;
        for (long x : type) {
            if (getCount(values, x) <= k) {
                ans = Math.max(ans, x);
            }
        }
        System.out.println(ans);
    }
    
    static long getCount(long[] values, long x) {
        long[] mod = new long[values.length];
        long[] minus = new long[values.length];
        for (int i = 0; i < values.length; i++) {
            mod[i] = values[i] % x;
        }
        Arrays.sort(mod);
        for (int i = 0; i < values.length; i++) {
            if (mod[i] > 0) {
                minus[i] = x - mod[i];
            }
            if (i > 0) {
                mod[i] += mod[i - 1];
            }
        }
        for (int i = values.length - 2; i >= 0; i--) {
            minus[i] += minus[i + 1];
        }
        long ans = Math.min(minus[0], mod[values.length - 1]);
        for (int i = 0; i < values.length - 1; i++) {
            ans = Math.min(ans, mod[i] + minus[i + 1]);
        }
        return ans;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0