結果

問題 No.1486 ロボット
ユーザー nephrologistnephrologist
提出日時 2021-04-24 12:56:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,974 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 77,792 KB
最終ジャッジ日時 2023-09-17 13:34:39
合計ジャッジ時間 3,273 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,556 KB
testcase_01 AC 82 ms
71,132 KB
testcase_02 AC 80 ms
71,400 KB
testcase_03 AC 81 ms
71,292 KB
testcase_04 AC 80 ms
71,396 KB
testcase_05 AC 81 ms
71,444 KB
testcase_06 AC 127 ms
77,612 KB
testcase_07 AC 128 ms
77,792 KB
testcase_08 AC 119 ms
77,552 KB
testcase_09 AC 80 ms
71,680 KB
testcase_10 AC 91 ms
75,872 KB
testcase_11 AC 87 ms
75,928 KB
testcase_12 AC 81 ms
71,352 KB
testcase_13 AC 82 ms
71,592 KB
testcase_14 AC 116 ms
77,384 KB
testcase_15 AC 105 ms
76,548 KB
testcase_16 AC 110 ms
77,356 KB
testcase_17 AC 113 ms
77,472 KB
testcase_18 AC 97 ms
76,000 KB
testcase_19 AC 93 ms
76,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappop, heappush
from math import gcd

input = sys.stdin.buffer.readline

a, b, c, d, e = map(int, input().split())
g = gcd(a + b, c + d)
lcm = (a + b) * (c + d) // g

temp = 0
events = []
while temp < lcm:
    heappush(events, (temp, 1))
    heappush(events, (temp + a, -1))
    temp += a + b

temp = 0
while temp < lcm:
    heappush(events, (temp, 2))
    heappush(events, (temp + c, -2))
    temp += c + d
# print("events1", events)


score_cycle = 0
flag1 = 0
flag2 = 0
now = 0
onflag = 0
while events:
    temp, num = heappop(events)
    # print("tn", temp, num)
    if num == 1:
        flag1 = 1
    elif num == 2:
        flag2 = 1
    elif num == -1:
        flag1 = 0
    else:
        flag2 = 0
    if flag1 == 1 and flag2 == 1:
        onflag = 1
        now = temp
    elif flag1 == 0:
        if onflag:
            score_cycle += temp - now
            onflag = 0
    elif flag2 == 0:
        if onflag:
            score_cycle += temp - now
            onflag = 0


events = []
lim = e % lcm
temp = 0
while temp <= lim:
    heappush(events, (temp, 1))
    temp += a
    if temp > lim:
        break
    heappush(events, (temp, -1))
    temp += b

temp = 0
while temp <= lim:
    heappush(events, (temp, 2))
    temp += c
    if temp > lim:
        break
    heappush(events, (temp, -2))
    temp += d
# print("events2", events)
score_rem = 0
flag1 = 0
flag2 = 0
now = 0
onflag = 0
while events:
    temp, num = heappop(events)
    if num == 1:
        flag1 = 1
    elif num == 2:
        flag2 = 1
    elif num == -1:
        flag1 = 0
    else:
        flag2 = 0
    if flag1 == 1 and flag2 == 1:
        onflag = 1
        now = temp
    elif flag1 == 0:
        if onflag:
            score_rem += temp - now
            onflag = 0
    elif flag2 == 0:
        if onflag:
            score_rem += temp - now
            onflag = 0
if onflag:
    score_rem += lim - now

ans = (e // lcm) * score_cycle + score_rem
print(ans)
0