結果

問題 No.3015 アンチローリングハッシュ
ユーザー kokoa1046kokoa1046
提出日時 2018-01-30 09:57:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 852 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 66 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 31,936 KB
最終ジャッジ日時 2023-08-28 16:34:40
合計ジャッジ時間 9,490 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
9,852 KB
testcase_01 AC 28 ms
9,816 KB
testcase_02 AC 27 ms
9,776 KB
testcase_03 AC 27 ms
9,720 KB
testcase_04 AC 29 ms
9,724 KB
testcase_05 AC 26 ms
9,704 KB
testcase_06 AC 40 ms
10,000 KB
testcase_07 AC 32 ms
9,748 KB
testcase_08 AC 203 ms
15,924 KB
testcase_09 AC 178 ms
15,196 KB
testcase_10 AC 423 ms
20,716 KB
testcase_11 AC 829 ms
31,876 KB
testcase_12 AC 852 ms
22,060 KB
testcase_13 AC 471 ms
16,192 KB
testcase_14 AC 149 ms
14,204 KB
testcase_15 AC 36 ms
9,820 KB
testcase_16 AC 810 ms
31,724 KB
testcase_17 AC 302 ms
17,312 KB
testcase_18 AC 640 ms
31,724 KB
testcase_19 AC 714 ms
31,936 KB
testcase_20 AC 637 ms
31,908 KB
testcase_21 AC 380 ms
20,592 KB
testcase_22 AC 86 ms
13,460 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