結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
66,024 KB
testcase_01 AC 59 ms
66,164 KB
testcase_02 AC 58 ms
66,780 KB
testcase_03 AC 60 ms
66,752 KB
testcase_04 AC 59 ms
67,300 KB
testcase_05 AC 58 ms
66,376 KB
testcase_06 AC 107 ms
78,884 KB
testcase_07 AC 61 ms
67,436 KB
testcase_08 AC 106 ms
79,088 KB
testcase_09 AC 259 ms
81,048 KB
testcase_10 AC 264 ms
81,644 KB
testcase_11 AC 634 ms
80,904 KB
testcase_12 AC 312 ms
81,280 KB
testcase_13 AC 254 ms
80,460 KB
testcase_14 AC 227 ms
80,224 KB
testcase_15 AC 104 ms
78,864 KB
testcase_16 AC 375 ms
81,284 KB
testcase_17 AC 361 ms
80,572 KB
testcase_18 AC 93 ms
78,432 KB
testcase_19 AC 73 ms
72,460 KB
testcase_20 AC 72 ms
70,728 KB
testcase_21 AC 157 ms
79,368 KB
testcase_22 AC 59 ms
66,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 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