結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-10 20:10:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 992 ms / 2,000 ms
コード長 1,057 bytes
コンパイル時間 138 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 81,084 KB
最終ジャッジ日時 2024-04-14 23:54:13
合計ジャッジ時間 6,560 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
65,580 KB
testcase_01 AC 62 ms
66,692 KB
testcase_02 AC 60 ms
65,964 KB
testcase_03 AC 59 ms
65,740 KB
testcase_04 AC 59 ms
65,972 KB
testcase_05 AC 59 ms
66,636 KB
testcase_06 AC 77 ms
74,060 KB
testcase_07 AC 63 ms
67,800 KB
testcase_08 AC 82 ms
74,724 KB
testcase_09 AC 224 ms
80,040 KB
testcase_10 AC 302 ms
80,488 KB
testcase_11 AC 767 ms
81,084 KB
testcase_12 AC 240 ms
80,000 KB
testcase_13 AC 276 ms
80,388 KB
testcase_14 AC 230 ms
80,792 KB
testcase_15 AC 92 ms
78,264 KB
testcase_16 AC 992 ms
80,752 KB
testcase_17 AC 810 ms
80,752 KB
testcase_18 AC 72 ms
73,340 KB
testcase_19 AC 94 ms
78,236 KB
testcase_20 AC 78 ms
74,584 KB
testcase_21 AC 107 ms
78,828 KB
testcase_22 AC 60 ms
65,676 KB
権限があれば一括ダウンロードができます

ソースコード

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