結果

問題 No.8015 アンチローリングハッシュ
ユーザー norioc
提出日時 2025-03-23 21:57:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 584 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 92,184 KB
最終ジャッジ日時 2025-03-23 21:57:38
合計ジャッジ時間 3,712 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

from string import ascii_lowercase
import random


def f(b, mod):
    # S[i] * b^i
    d = {}
    for i in range(mod+1):
        x = pow(b, i, mod)
        if x in d:
            return i, d[x]

        d[x] = i

    assert False


def myhash(s: str, b, mod):
    h = 0
    for c in s:
        h = h*b + ord(c)
        h %= mod

    return h


A, B = map(int, input().split())

x, y = f(A, B)
n = max(x, y) + 1
s = random.choices(ascii_lowercase, k=n)
t = s[:]

p = n-1-x
q = n-1-y
t[p], t[q] = t[q], t[p]
assert myhash(s, A, B) == myhash(t, A, B)
print(''.join(s))
print(''.join(t))
0