結果

問題 No.3015 アンチローリングハッシュ
ユーザー kokoa1046kokoa1046
提出日時 2018-01-30 09:57:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 532 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 113,956 KB
最終ジャッジ日時 2023-08-28 16:34:51
合計ジャッジ時間 8,245 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
79,612 KB
testcase_01 AC 161 ms
79,684 KB
testcase_02 AC 158 ms
79,568 KB
testcase_03 AC 159 ms
79,680 KB
testcase_04 AC 161 ms
79,708 KB
testcase_05 AC 162 ms
79,992 KB
testcase_06 AC 216 ms
82,244 KB
testcase_07 AC 187 ms
82,016 KB
testcase_08 AC 260 ms
90,456 KB
testcase_09 AC 307 ms
89,488 KB
testcase_10 AC 328 ms
95,384 KB
testcase_11 AC 404 ms
106,892 KB
testcase_12 AC 342 ms
99,536 KB
testcase_13 AC 305 ms
91,560 KB
testcase_14 AC 248 ms
86,980 KB
testcase_15 AC 204 ms
82,396 KB
testcase_16 AC 532 ms
111,044 KB
testcase_17 AC 288 ms
92,948 KB
testcase_18 AC 495 ms
113,592 KB
testcase_19 AC 443 ms
113,956 KB
testcase_20 AC 403 ms
113,924 KB
testcase_21 AC 311 ms
95,844 KB
testcase_22 AC 211 ms
83,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import itertools
import random
import string


MAX_L = 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 birthday_attack(a, b, digits=MAX_L):
    candidates = {generate_random_string(digits) for _ in range(2 * b)}
    f = lambda x: compute_hash(x, a, b)
    for s, t in itertools.combinations(candidates, r=2):
        if f(s) == f(t):
            return s, t
    else:
        assert False


def main():
    a, b = map(int, input().split())
    s, t = birthday_attack(a, b)
    print(s)
    print(t)


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