結果

問題 No.295 hel__world
ユーザー gew1fw
提出日時 2025-06-12 12:55:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,065 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 154,376 KB
最終ジャッジ日時 2025-06-12 13:01:00
合計ジャッジ時間 5,305 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def multiply_with_overflow(x, y):
    if x == 0 or y == 0:
        return 0
    if x > (2**62) // y:
        return None
    return x * y

def pow_with_overflow(a, b):
    result = 1
    current = a
    while b > 0:
        if b % 2 == 1:
            temp = multiply_with_overflow(result, current)
            if temp is None:
                return None
            result = temp
        temp = multiply_with_overflow(current, current)
        if temp is None:
            return None
        current = temp
        b = b // 2
    return result

def main():
    S = list(map(int, sys.stdin.readline().split()))
    T = sys.stdin.readline().strip()

    # Process T into runs_t
    runs_t = []
    prev = None
    for c in T:
        if c != prev:
            runs_t.append(c)
            prev = c

    # Compute frequency of each character in runs_t
    from collections import defaultdict
    freq = defaultdict(int)
    for c in runs_t:
        freq[c] += 1

    # Check if S has enough for each character
    S_dict = {chr(ord('a') + i): S[i] for i in range(26)}
    for c in freq:
        if S_dict[c] < freq[c]:
            print(0)
            return

    # Compute the product
    total = 1
    for c in freq:
        required = freq[c]
        available = S_dict[c] - required
        k = required
        m = available
        q = m // k
        r = m % k

        # Compute part1 = (q+2)^r
        part1 = pow_with_overflow(q + 2, r)
        # Compute part2 = (q+1)^(k - r)
        part2 = pow_with_overflow(q + 1, k - r)

        if part1 is None or part2 is None:
            print("hel")
            return

        contribution = multiply_with_overflow(part1, part2)
        if contribution is None:
            print("hel")
            return

        new_total = multiply_with_overflow(total, contribution)
        if new_total is None:
            print("hel")
            return
        total = new_total

        if total > (2**62):
            print("hel")
            return

    print(total)

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