結果

問題 No.2968 Final MIGISITA Strike
ユーザー 👑 rin204rin204
提出日時 2024-11-16 22:54:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,133 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 121,352 KB
最終ジャッジ日時 2024-11-16 22:54:40
合計ジャッジ時間 26,301 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 5 WA * 62
権限があれば一括ダウンロードができます

ソースコード

diff #

def ext_gcd(a, b):
    """
    return (x, y, gcd(a, b)) s.t. ax + by = gcd(a, b)
    """
    if b == 0:
        return 1, 0, a
    else:
        y, x, g = ext_gcd(b, a % b)
        return x, y - (a // b) * x, g
from math import gcd

MOD = 998244353

h, w, n, m, s, c, X, Y = map(int, input().split())
pos = []
for _ in range(n):
    x, y, a = map(int, input().split())
    y = w - y
    pos.append((x, y, a))

for _ in range(m):
    x, y, a = map(int, input().split())
    y = w - y
    pos.append((x, y, -a))

Y = w - Y

lcm = 4 * h * w // gcd(2 * h, 2 * w)
add = []

c0 = 0
c1 = 0

for x, y, a in pos:
    x_, y_, g = ext_gcd(2 * h, 2 * w)
    diff = (X - Y) - (x - y)

    if diff % g == 0:
        xx = x_ * (diff // g)
        to_x = x + xx * 2 * h
        add.append(((to_x - X) % lcm, a))
        if a > 0:
            c0 += 1
        else:
            c1 += 1

    diff = (X - Y) - (x - (2 * w - y))
    if diff % g == 0:
        xx = x_ * (diff // g)
        to_x = x + xx * 2 * h
        add.append(((to_x - X) % lcm, a))
        if a > 0:
            c0 += 1
        else:
            c1 += 1

    diff = (X - Y) - ((2 * h - x) - y)
    if diff % g == 0:
        xx = x_ * (diff // g)
        to_x = 2 * h - x + xx * 2 * h
        add.append(((to_x - X) % lcm, a))
        if a > 0:
            c0 += 1
        else:
            c1 += 1

    diff = (X - Y) - ((2 * h - x) - (2 * w - y))
    if diff % g == 0:
        xx = x_ * (diff // g)
        to_x = 2 * h - x + xx * 2 * h
        add.append(((to_x - X) % lcm, a))
        if a > 0:
            c0 += 1
        else:
            c1 += 1

add.sort(key=lambda x: x[0])

tot = 0
min_ = 0

add.append((lcm, 0))
bt = -1
for t, x in add:
    tot -= (t - 1 + X) // h * c
    tot -= (t - 1 + Y) // w * c
    tot += (bt + X) // h * c
    tot += (bt + Y) // w * c

    min_ = min(min_, tot)
    tot += x

    bt = t

if s + min_ > 0 and tot >= 0:
    print(-1)
    exit()

loop = max(0, (s + min_) // abs(tot))


c0 *= loop
c1 *= loop
s += loop * tot
bt = -1

for t, x in add:
    diff = 0
    diff -= (t - 1 + X) // h * c
    diff -= (t - 1 + Y) // w * c
    diff += (bt + X) // h * c
    diff += (bt + Y) // w * c
    bt = t

    if diff + s <= 0:
        lef = bt
        rig = t
        while rig - lef > 1:
            mid = (rig + lef) // 2
            diff = 0
            diff -= (mid - 1 + X) // h * c
            diff -= (mid - 1 + Y) // w * c
            diff += (bt + X) // h * c
            diff += (bt + Y) // w * c
            if diff + s <= 0:
                rig = mid
            else:
                lef = mid

        xx = (X + rig) % (2 * h)
        yy = (Y + rig) % (2 * w)
        if xx >= h:
            xx = 2 * h - xx
        if yy >= w:
            yy = 2 * w - yy
        yy = w - yy
        print(xx, yy, c0, c1)
        break

    s += diff
    s += x

    if x > 0:
        c0 += 1
    else:
        c1 += 1

    if s <= 0:
        xx = (X + t) % (2 * h)
        yy = (Y + t) % (2 * w)
        if xx >= h:
            xx = 2 * h - xx
        if yy >= w:
            yy = 2 * w - yy
        yy = w - yy
        print(xx, yy, c0, c1)
        break
0