結果

問題 No.1935 Water Simulation
ユーザー lam6er
提出日時 2025-03-20 21:12:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,606 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 59,980 KB
最終ジャッジ日時 2025-03-20 21:14:09
合計ジャッジ時間 2,756 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

V1, V2, V3, V4, N = map(int, input().split())

v = [V1, V2, V3, V4]
a = [v[0], 0, 0, 0]
current_step = 0
next_op = 1  # after 0 steps, the first operation is 1
state_map = {}
state_key = (tuple(a), next_op)
state_map[state_key] = current_step
found_cycle = False

while current_step < N:
    # Perform the current operation
    current_op = next_op
    if current_op == 1:
        # Pour from 1 to 2 (indices 0 to 1)
        transfer = min(a[0], v[1] - a[1])
        a[0] -= transfer
        a[1] += transfer
    elif current_op == 2:
        # Pour from 2 to 3 (indices 1 to 2)
        transfer = min(a[1], v[2] - a[2])
        a[1] -= transfer
        a[2] += transfer
    elif current_op == 3:
        # Pour from 3 to 4 (indices 2 to 3)
        transfer = min(a[2], v[3] - a[3])
        a[2] -= transfer
        a[3] += transfer
    else:
        # Pour from 4 to 1 (indices 3 to 0)
        transfer = min(a[3], v[0] - a[0])
        a[3] -= transfer
        a[0] += transfer

    current_step += 1
    if current_step == N:
        break

    # Determine next_op for the next step
    next_op = (current_step % 4) + 1
    new_key = (tuple(a), next_op)

    # Check for cycle
    if new_key in state_map:
        prev_step = state_map[new_key]
        cycle_len = current_step - prev_step
        remaining_steps = N - current_step

        cycles = remaining_steps // cycle_len
        current_step += cycles * cycle_len

        # Update to the state that started the cycle
        a = list(new_key[0])
        next_op = new_key[1]

        # Handle remaining steps after cycles
        if current_step < N:
            steps_rem = N - current_step
            for _ in range(steps_rem):
                curr_op = next_op
                if curr_op == 1:
                    transfer = min(a[0], v[1] - a[1])
                    a[0] -= transfer
                    a[1] += transfer
                elif curr_op == 2:
                    transfer = min(a[1], v[2] - a[2])
                    a[1] -= transfer
                    a[2] += transfer
                elif curr_op == 3:
                    transfer = min(a[2], v[3] - a[3])
                    a[2] -= transfer
                    a[3] += transfer
                else:
                    transfer = min(a[3], v[0] - a[0])
                    a[3] -= transfer
                    a[0] += transfer
                current_step += 1
                if current_step >= N:
                    break
                next_op = (current_step % 4) + 1
        break
    else:
        state_map[new_key] = current_step

print(' '.join(map(str, a)))
0