結果

問題 No.3015 アンチローリングハッシュ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-10 20:03:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 1,000 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 81,492 KB
最終ジャッジ日時 2024-04-14 23:53:09
合計ジャッジ時間 5,603 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
66,856 KB
testcase_01 AC 59 ms
66,852 KB
testcase_02 AC 61 ms
65,560 KB
testcase_03 AC 57 ms
66,292 KB
testcase_04 AC 59 ms
66,120 KB
testcase_05 AC 60 ms
66,920 KB
testcase_06 AC 70 ms
71,648 KB
testcase_07 AC 79 ms
75,388 KB
testcase_08 AC 77 ms
74,472 KB
testcase_09 AC 208 ms
80,632 KB
testcase_10 AC 357 ms
80,480 KB
testcase_11 AC 372 ms
81,492 KB
testcase_12 AC 249 ms
79,968 KB
testcase_13 AC 242 ms
80,328 KB
testcase_14 AC 181 ms
79,972 KB
testcase_15 AC 149 ms
79,452 KB
testcase_16 AC 368 ms
80,700 KB
testcase_17 AC 248 ms
81,216 KB
testcase_18 AC 169 ms
79,564 KB
testcase_19 AC 162 ms
80,080 KB
testcase_20 AC 102 ms
78,416 KB
testcase_21 AC 144 ms
79,692 KB
testcase_22 AC 60 ms
66,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import random
import string


MAX_L = 15


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