結果

問題 No.8015 アンチローリングハッシュ
ユーザー はむ吉🐹
提出日時 2016-04-10 20:10:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,297 ms / 2,000 ms
コード長 1,057 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 80,916 KB
最終ジャッジ日時 2024-10-04 05:53:34
合計ジャッジ時間 7,380 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3
# -*- coding: utf-8 -*-

import math
import random
import string


MAX_L = 4


def compute_hash(s, a, b):
    h = 0
    n = len(s)
    for i, c in enumerate(s):
        h += ((ord(c) % b) * pow(a, n - 1 - i, b)) % b
        h %= b
    return h


def generate_random_string(digits, source=string.ascii_lowercase):
    return "".join(random.choice(source) for _ in range(digits))


def generate_random_pair(digits):
    s = ""
    t = ""
    while s == t:
        s = generate_random_string(digits)
        t = generate_random_string(digits)
    return s, t


def birthday_attack(a, b, digits_lim=MAX_L):
    hash_s = -1
    hash_t = -2
    f = lambda x: compute_hash(x, a, b)
    while hash_s != hash_t:
        d = random.randint(1, digits_lim)
        s, t = generate_random_pair(d)
        hash_s, hash_t = map(f, (s, t))
    return s, t


def main():
    a, b = map(int, input().split())
    d = max(2, math.ceil(math.log(b, 26)))
    s, t = birthday_attack(a, b, d)
    print(s)
    print(t)


if __name__ == '__main__':
    main()
0