結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-12 21:49:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 846 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 82,664 KB
実行使用メモリ 111,392 KB
最終ジャッジ日時 2024-04-15 00:44:43
合計ジャッジ時間 6,074 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
65,536 KB
testcase_01 AC 65 ms
65,536 KB
testcase_02 AC 61 ms
65,536 KB
testcase_03 AC 61 ms
66,048 KB
testcase_04 AC 62 ms
65,664 KB
testcase_05 AC 61 ms
65,476 KB
testcase_06 AC 117 ms
79,232 KB
testcase_07 AC 95 ms
78,592 KB
testcase_08 AC 183 ms
85,248 KB
testcase_09 AC 192 ms
86,400 KB
testcase_10 AC 228 ms
92,672 KB
testcase_11 AC 346 ms
109,952 KB
testcase_12 AC 287 ms
96,424 KB
testcase_13 AC 203 ms
88,192 KB
testcase_14 AC 161 ms
83,660 KB
testcase_15 AC 96 ms
79,000 KB
testcase_16 AC 321 ms
107,832 KB
testcase_17 AC 227 ms
89,984 KB
testcase_18 AC 440 ms
111,152 KB
testcase_19 AC 361 ms
111,384 KB
testcase_20 AC 353 ms
111,392 KB
testcase_21 AC 237 ms
92,500 KB
testcase_22 AC 119 ms
80,440 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