結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-10 19:59:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 776 ms / 2,000 ms
コード長 1,002 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-14 23:52:26
合計ジャッジ時間 5,995 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
11,136 KB
testcase_01 AC 35 ms
11,136 KB
testcase_02 AC 38 ms
11,136 KB
testcase_03 AC 34 ms
11,264 KB
testcase_04 AC 35 ms
11,136 KB
testcase_05 AC 34 ms
11,136 KB
testcase_06 AC 37 ms
11,264 KB
testcase_07 AC 35 ms
11,392 KB
testcase_08 AC 38 ms
11,136 KB
testcase_09 AC 234 ms
11,264 KB
testcase_10 AC 41 ms
11,392 KB
testcase_11 AC 56 ms
11,136 KB
testcase_12 AC 205 ms
11,136 KB
testcase_13 AC 776 ms
11,392 KB
testcase_14 AC 369 ms
11,264 KB
testcase_15 AC 36 ms
11,264 KB
testcase_16 AC 102 ms
11,264 KB
testcase_17 AC 194 ms
11,264 KB
testcase_18 AC 87 ms
11,264 KB
testcase_19 AC 47 ms
11,136 KB
testcase_20 AC 37 ms
11,392 KB
testcase_21 AC 70 ms
11,264 KB
testcase_22 AC 35 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import random
import string


MAX_L = 10


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())
    s, t = birthday_attack(a, b)
    print(s)
    print(t)


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