結果

問題 No.1972 Modulo Set
コンテスト
ユーザー lam6er
提出日時 2025-03-31 17:36:25
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,169 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 224 ms
コンパイル使用メモリ 95,844 KB
実行使用メモリ 119,736 KB
最終ジャッジ日時 2026-07-08 04:42:29
合計ジャッジ時間 6,578 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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