結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 159 ms
79,856 KB
testcase_01 AC 159 ms
79,868 KB
testcase_02 AC 161 ms
79,764 KB
testcase_03 AC 161 ms
79,868 KB
testcase_04 AC 160 ms
79,768 KB
testcase_05 AC 158 ms
79,936 KB
testcase_06 AC 208 ms
82,752 KB
testcase_07 AC 188 ms
81,808 KB
testcase_08 AC 260 ms
88,588 KB
testcase_09 AC 280 ms
89,324 KB
testcase_10 AC 294 ms
93,324 KB
testcase_11 AC 493 ms
114,048 KB
testcase_12 AC 345 ms
100,284 KB
testcase_13 AC 255 ms
88,672 KB
testcase_14 AC 276 ms
86,884 KB
testcase_15 AC 189 ms
82,492 KB
testcase_16 AC 481 ms
111,076 KB
testcase_17 AC 315 ms
92,840 KB
testcase_18 AC 455 ms
114,892 KB
testcase_19 AC 477 ms
114,792 KB
testcase_20 AC 484 ms
114,832 KB
testcase_21 AC 306 ms
96,060 KB
testcase_22 AC 210 ms
83,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import itertools
import random
import string


MAX_L = 6


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