結果

問題 No.1972 Modulo Set
ユーザー lam6er
提出日時 2025-03-31 17:36:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,169 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 122,412 KB
最終ジャッジ日時 2025-03-31 17:36:55
合計ジャッジ時間 5,042 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1
    A = list(map(int, input[idx:idx+N]))
    idx += N

    freq = defaultdict(int)
    for num in A:
        r = num % M
        freq[r] += 1

    processed = set()
    ans = 0

    # Handle residue 0
    if 0 in freq:
        ans += 1
        processed.add(0)

    # Process other residues
    for r in list(freq.keys()):
        if r in processed:
            continue
        if r == 0:
            continue
        s = (M - r) % M
        if r == s:
            # Self-complementary, e.g., M even and r = M/2
            ans += 1
            processed.add(r)
        elif s in freq:
            # Take the maximum of the two residues
            ans += max(freq[r], freq[s])
            processed.add(r)
            processed.add(s)
        else:
            # Check if 2*r is a multiple of M
            if (2 * r) % M == 0:
                ans += 1
            else:
                ans += freq[r]
            processed.add(r)
    print(ans)

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