結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-10 19:57:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,007 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 20,340 KB
最終ジャッジ日時 2024-04-14 23:52:18
合計ジャッジ時間 6,491 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
18,692 KB
testcase_01 AC 373 ms
12,160 KB
testcase_02 AC 1,685 ms
13,088 KB
testcase_03 AC 234 ms
12,252 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import random
import string


MAX_L = 10 ** 5


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())
    s, t = birthday_attack(a, b)
    print(s)
    print(t)


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