結果
問題 | No.2570 最大最大公約数 |
ユーザー |
![]() |
提出日時 | 2023-12-05 10:59:08 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 739 ms / 2,000 ms |
コード長 | 2,855 bytes |
コンパイル時間 | 2,862 ms |
コンパイル使用メモリ | 96,872 KB |
実行使用メモリ | 57,188 KB |
最終ジャッジ日時 | 2024-11-13 17:16:28 |
合計ジャッジ時間 | 8,946 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 28 |
ソースコード
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(); } }