結果

問題 No.2968 Final MIGISITA Strike
ユーザー 👑 rin204rin204
提出日時 2024-11-16 23:06:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 872 ms / 2,525 ms
コード長 3,215 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 121,064 KB
最終ジャッジ日時 2024-11-16 23:06:58
合計ジャッジ時間 23,349 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,120 KB
testcase_01 AC 35 ms
52,736 KB
testcase_02 AC 37 ms
52,992 KB
testcase_03 AC 36 ms
53,248 KB
testcase_04 AC 35 ms
52,864 KB
testcase_05 AC 37 ms
52,992 KB
testcase_06 AC 37 ms
52,992 KB
testcase_07 AC 35 ms
52,480 KB
testcase_08 AC 35 ms
52,864 KB
testcase_09 AC 35 ms
53,120 KB
testcase_10 AC 371 ms
99,528 KB
testcase_11 AC 430 ms
107,316 KB
testcase_12 AC 213 ms
86,400 KB
testcase_13 AC 288 ms
91,460 KB
testcase_14 AC 198 ms
86,272 KB
testcase_15 AC 687 ms
110,188 KB
testcase_16 AC 580 ms
107,432 KB
testcase_17 AC 522 ms
101,344 KB
testcase_18 AC 382 ms
96,188 KB
testcase_19 AC 481 ms
99,556 KB
testcase_20 AC 594 ms
107,760 KB
testcase_21 AC 251 ms
85,120 KB
testcase_22 AC 342 ms
89,600 KB
testcase_23 AC 712 ms
117,424 KB
testcase_24 AC 696 ms
111,548 KB
testcase_25 AC 872 ms
121,064 KB
testcase_26 AC 358 ms
89,852 KB
testcase_27 AC 523 ms
96,052 KB
testcase_28 AC 386 ms
89,900 KB
testcase_29 AC 447 ms
94,320 KB
testcase_30 AC 144 ms
84,432 KB
testcase_31 AC 140 ms
83,584 KB
testcase_32 AC 154 ms
87,040 KB
testcase_33 AC 142 ms
83,828 KB
testcase_34 AC 358 ms
89,388 KB
testcase_35 AC 338 ms
87,868 KB
testcase_36 AC 168 ms
78,984 KB
testcase_37 AC 281 ms
86,796 KB
testcase_38 AC 305 ms
87,996 KB
testcase_39 AC 305 ms
87,168 KB
testcase_40 AC 354 ms
92,436 KB
testcase_41 AC 279 ms
90,028 KB
testcase_42 AC 254 ms
86,268 KB
testcase_43 AC 349 ms
89,976 KB
testcase_44 AC 422 ms
97,400 KB
testcase_45 AC 215 ms
81,408 KB
testcase_46 AC 510 ms
103,736 KB
testcase_47 AC 362 ms
93,312 KB
testcase_48 AC 399 ms
98,276 KB
testcase_49 AC 60 ms
68,992 KB
testcase_50 AC 246 ms
83,712 KB
testcase_51 AC 345 ms
90,560 KB
testcase_52 AC 320 ms
92,112 KB
testcase_53 AC 404 ms
93,632 KB
testcase_54 AC 244 ms
82,420 KB
testcase_55 AC 504 ms
101,084 KB
testcase_56 AC 283 ms
87,908 KB
testcase_57 AC 476 ms
100,364 KB
testcase_58 AC 396 ms
96,800 KB
testcase_59 AC 279 ms
87,464 KB
testcase_60 AC 243 ms
84,480 KB
testcase_61 AC 259 ms
83,236 KB
testcase_62 AC 388 ms
95,240 KB
testcase_63 AC 395 ms
94,752 KB
testcase_64 AC 36 ms
52,480 KB
testcase_65 AC 176 ms
89,608 KB
testcase_66 AC 185 ms
92,920 KB
権限があれば一括ダウンロードができます

ソースコード

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) - 1) // 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

    if diff + s <= 0:
        lef = bt
        rig = t
        while rig - lef > 1:
            mid = (rig + lef) // 2
            diff = 0
            diff -= (mid + X) // h * c
            diff -= (mid + 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
        c0 %= MOD
        c1 %= MOD
        print(xx, yy, c0, c1)
        break
    bt = t

    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
        c0 %= MOD
        c1 %= MOD
        print(xx, yy, c0, c1)
        break
0