結果

問題 No.1648 Sum of Powers
ユーザー MitarushiMitarushi
提出日時 2021-07-29 15:47:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 99,916 KB
最終ジャッジ日時 2024-04-14 16:07:20
合計ジャッジ時間 9,409 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
99,500 KB
testcase_01 AC 68 ms
75,784 KB
testcase_02 AC 121 ms
99,632 KB
testcase_03 AC 114 ms
99,656 KB
testcase_04 AC 121 ms
99,764 KB
testcase_05 AC 119 ms
99,524 KB
testcase_06 AC 121 ms
99,916 KB
testcase_07 AC 117 ms
99,648 KB
testcase_08 AC 111 ms
99,520 KB
testcase_09 AC 121 ms
99,880 KB
testcase_10 AC 121 ms
99,892 KB
testcase_11 AC 123 ms
99,388 KB
testcase_12 AC 124 ms
99,900 KB
testcase_13 AC 115 ms
99,764 KB
testcase_14 AC 118 ms
99,448 KB
testcase_15 AC 120 ms
99,904 KB
testcase_16 AC 124 ms
99,900 KB
testcase_17 AC 119 ms
99,652 KB
testcase_18 AC 121 ms
99,644 KB
testcase_19 AC 120 ms
99,396 KB
testcase_20 AC 116 ms
99,656 KB
testcase_21 AC 120 ms
99,776 KB
testcase_22 AC 114 ms
99,644 KB
testcase_23 AC 117 ms
99,908 KB
testcase_24 AC 122 ms
99,892 KB
testcase_25 AC 121 ms
99,648 KB
testcase_26 AC 118 ms
99,652 KB
testcase_27 AC 115 ms
99,784 KB
testcase_28 AC 119 ms
99,636 KB
testcase_29 AC 122 ms
99,648 KB
testcase_30 AC 122 ms
99,504 KB
testcase_31 AC 144 ms
99,904 KB
testcase_32 AC 139 ms
99,648 KB
testcase_33 AC 147 ms
99,404 KB
testcase_34 AC 122 ms
99,380 KB
testcase_35 AC 120 ms
99,756 KB
testcase_36 AC 123 ms
99,632 KB
testcase_37 AC 139 ms
99,892 KB
testcase_38 AC 143 ms
99,844 KB
testcase_39 AC 116 ms
99,776 KB
testcase_40 AC 123 ms
99,632 KB
testcase_41 AC 137 ms
99,912 KB
testcase_42 AC 133 ms
99,912 KB
testcase_43 AC 123 ms
99,788 KB
testcase_44 AC 134 ms
99,884 KB
testcase_45 AC 135 ms
99,644 KB
testcase_46 AC 122 ms
99,632 KB
testcase_47 AC 114 ms
99,576 KB
testcase_48 AC 69 ms
75,576 KB
testcase_49 AC 112 ms
99,656 KB
testcase_50 AC 123 ms
99,776 KB
testcase_51 AC 69 ms
75,704 KB
testcase_52 AC 114 ms
99,628 KB
testcase_53 AC 117 ms
99,652 KB
testcase_54 AC 116 ms
99,376 KB
testcase_55 AC 132 ms
99,660 KB
testcase_56 AC 132 ms
99,892 KB
testcase_57 AC 69 ms
75,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def mul(l, r):
    la, lb, lc, ld = l
    ra, rb, rc, rd = r
    return (la*ra+lb*rc) % mod, (la*rb+lb*rd) % mod, (lc*ra+ld*rc) % mod, (lc*rb+ld*rd) % mod


def inv(x):
    a, b, c, d = x
    det = (a * d - b * c) % mod
    det_inv = pow(det, mod-2, mod)
    y = d, -b, -c, a
    return tuple(i * det_inv % mod for i in y)


def mat_pow(a, x):
    b = 1, 0, 0, 1
    while x:
        if x % 2 == 1:
            b = mul(b, a)
        a = mul(a, a)
        x //= 2
    return b


def bsgs(mat, a, b):
    k = 10 ** 5 + 100

    mat_k = mat_pow(mat, k)
    giant = {}
    x = a
    for i in range(k):
        giant[x] = i
        x = mul(mat_k, x)

    mat_inv = inv(mat)
    x = b
    for i in range(k):
        if x in giant:
            return giant[x] * k + i

        x = mul(mat_inv, x)

    return None


a, b, p, q = map(int, input().split())

if b != 0:
    mat = a, -b, 1, 0
else:
    mat = a, 0, 0, a

start = (a ** 2 - 2 * b) % mod, 0, a, 0
finish = p, 0, q, 0
n = bsgs(mat, start, finish) + 2
print(n)
0