結果

問題 No.1884 Sequence
ユーザー tenten
提出日時 2022-04-02 16:57:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 699 ms / 2,000 ms
コード長 2,375 bytes
コンパイル時間 2,682 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 57,720 KB
最終ジャッジ日時 2024-11-21 18:08:46
合計ジャッジ時間 19,367 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int zeros = 0;
        ArrayList<Long> values = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            long x = sc.nextLong();
            if (x == 0) {
                zeros++;
            } else {
                values.add(x);
            }
        }
        if (values.size() == 0) {
            System.out.println("Yes");
            return;
        }
        Collections.sort(values);
        HashMap<Long, Integer> counts = new HashMap<>(); 
        for (int i = 0; i < values.size() - 1; i++) {
            long x = values.get(i + 1) - values.get(i);
            counts.put(x, counts.getOrDefault(x, 0) + 1);
        }
        if (counts.containsKey(0L)) {
            if (counts.size() == 1) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
            return;
        }
        long gcd = 0;
        for (long x : counts.keySet()) {
            if (gcd == 0) {
                gcd = x;
            } else {
                gcd = getGCD(gcd, x);
            }
        }
        long sum = 0;
        for (long x : counts.keySet()) {
            sum += (x / gcd - 1) * counts.get(x);
        }
        if (sum <= zeros) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
    
    static long getGCD(long x, long y) {
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
}


class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0