結果

問題 No.321 (P,Q)-サンタと街の子供たち
ユーザー lam6er
提出日時 2025-03-31 17:51:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,022 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 100,724 KB
最終ジャッジ日時 2025-03-31 17:52:28
合計ジャッジ時間 5,201 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def main():
    input = sys.stdin.read().split()
    idx = 0
    P = int(input[idx]); idx +=1
    Q = int(input[idx]); idx +=1
    N = int(input[idx]); idx +=1
    points = []
    for _ in range(N):
        x = int(input[idx]); idx +=1
        y = int(input[idx]); idx +=1
        points.append((x, y))
    
    count = 0
    
    if P == 0 and Q == 0:
        for x, y in points:
            if x == 0 and y == 0:
                count +=1
        print(count)
        return
    
    # Compute d = gcd(P, Q)
    d = math.gcd(P, Q)
    
    # Compute g = gcd(P+Q, P-Q)
    a = P + Q
    b = P - Q
    g = math.gcd(abs(a), abs(b))
    
    for x, y in points:
        # Check if x and y are multiples of d
        if x % d != 0 or y % d != 0:
            continue
        
        # Check sum and difference
        s = x + y
        t = x - y
        
        if s % g != 0 or t % g != 0:
            continue
        
        count +=1
    
    print(count)

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