結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-10 20:08:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 569 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,220 KB
実行使用メモリ 81,284 KB
最終ジャッジ日時 2024-04-14 23:54:37
合計ジャッジ時間 5,766 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
66,240 KB
testcase_01 AC 58 ms
66,376 KB
testcase_02 AC 58 ms
65,772 KB
testcase_03 AC 58 ms
66,292 KB
testcase_04 AC 58 ms
66,312 KB
testcase_05 AC 58 ms
66,164 KB
testcase_06 AC 80 ms
75,648 KB
testcase_07 AC 59 ms
65,844 KB
testcase_08 AC 78 ms
74,320 KB
testcase_09 AC 229 ms
80,528 KB
testcase_10 AC 569 ms
81,284 KB
testcase_11 AC 277 ms
80,996 KB
testcase_12 AC 280 ms
80,564 KB
testcase_13 AC 274 ms
80,272 KB
testcase_14 AC 222 ms
80,460 KB
testcase_15 AC 74 ms
73,148 KB
testcase_16 AC 357 ms
81,072 KB
testcase_17 AC 390 ms
80,928 KB
testcase_18 AC 152 ms
80,104 KB
testcase_19 AC 94 ms
78,404 KB
testcase_20 AC 59 ms
66,148 KB
testcase_21 AC 205 ms
80,660 KB
testcase_22 AC 60 ms
66,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import random
import string


MAX_L = 4


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