結果

問題 No.1648 Sum of Powers
ユーザー Mitarushi
提出日時 2021-07-29 15:47:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 99,964 KB
最終ジャッジ日時 2024-10-03 16:07:47
合計ジャッジ時間 8,002 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

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