結果

問題 No.1515 Making Many Multiples
ユーザー gew1fw
提出日時 2025-06-12 18:26:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,729 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 73,944 KB
最終ジャッジ日時 2025-06-12 18:26:46
合計ジャッジ時間 3,866 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 2 TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    K = int(input[idx]); idx +=1
    X = int(input[idx]); idx +=1
    Y = int(input[idx]); idx +=1
    A = list(map(int, input[idx:idx+N]))
    
    X_mod = X % K
    Y_mod = Y % K
    initial_r1, initial_r2 = sorted((X_mod, Y_mod))
    current_dict = {(initial_r1, initial_r2): 0}
    
    for a in A:
        a_mod = a % K
        next_dict = {}
        for (r1, r2), points in current_dict.items():
            sum_three = (r1 + r2 + a_mod) % K
            gain = 1 if sum_three == 0 else 0
            new_points = points + gain
            
            # Discard r1
            new_state = tuple(sorted((r2, a_mod)))
            if new_state in next_dict:
                if new_points > next_dict[new_state]:
                    next_dict[new_state] = new_points
            else:
                next_dict[new_state] = new_points
            
            # Discard r2
            new_state = tuple(sorted((r1, a_mod)))
            if new_state in next_dict:
                if new_points > next_dict[new_state]:
                    next_dict[new_state] = new_points
            else:
                next_dict[new_state] = new_points
            
            # Discard a_mod
            new_state = tuple(sorted((r1, r2)))
            if new_state in next_dict:
                if new_points > next_dict[new_state]:
                    next_dict[new_state] = new_points
            else:
                next_dict[new_state] = new_points
        
        current_dict = next_dict
    
    if current_dict:
        print(max(current_dict.values()))
    else:
        print(0)

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