結果

問題 No.1884 Sequence
ユーザー gew1fw
提出日時 2025-06-12 16:02:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,179 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 82,900 KB
実行使用メモリ 146,040 KB
最終ジャッジ日時 2025-06-12 16:02:19
合計ジャッジ時間 6,304 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def input():
    return sys.stdin.read()

def compute_gcd(arr):
    from math import gcd
    current_gcd = arr[0]
    for num in arr[1:]:
        current_gcd = gcd(current_gcd, num)
        if current_gcd == 0:
            break
    return current_gcd

def factorize(n):
    factors = {}
    while n % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        n = n // 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] = factors.get(i, 0) + 1
            n = n // i
        i += 2
    if n > 1:
        factors[n] = 1
    return factors

def get_divisors(n):
    if n == 0:
        return []
    factors = factorize(n)
    divisors = [1]
    for p in factors:
        exponents = factors[p]
        temp = []
        current_length = len(divisors)
        for e in range(1, exponents + 1):
            pe = p ** e
            for i in range(current_length):
                temp.append(divisors[i] * pe)
        divisors += temp
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

def main():
    data = input().split()
    N = int(data[0])
    A = list(map(int, data[1:N+1]))
    
    S = [x for x in A if x != 0]
    if len(S) <= 1:
        print("Yes")
        return
    
    S.sort()
    diffs = []
    for i in range(len(S)-1):
        diffs.append(S[i+1] - S[i])
    
    g = compute_gcd(diffs)
    if g == 0:
        print("Yes")
        return
    
    divisors = get_divisors(g)
    max_x = S[-1]
    min_x = S[0]
    required_length = N
    
    for d in divisors:
        if d == 0:
            continue
        if (max_x - min_x) > (required_length - 1) * d:
            continue
        a = max_x - (required_length - 1) * d
        if a <= 0 or a > min_x:
            continue
        
        valid = True
        for x in S:
            delta = x - a
            if delta % d != 0:
                valid = False
                break
            t = delta // d
            if t < 0 or t >= required_length:
                valid = False
                break
        if valid:
            print("Yes")
            return
    
    print("No")

if __name__ == "__main__":
    main()
0