結果

問題 No.16 累乗の加算
ユーザー Daisuke MiyakawaDaisuke Miyakawa
提出日時 2022-06-18 08:36:11
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,062 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 82,512 KB
最終ジャッジ日時 2024-04-18 01:44:13
合計ジャッジ時間 3,093 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import unittest


def repeated_squaring_sum(x, A, p):
    """(x^A1 + x^A2 ... + x^An) mod p を求める"""
    a_max = int(math.log2(max(A))) + 1
    cache = [1, x]
    i = 2
    while i <= a_max:
        cache.append((cache[i - 1] ** 2) % p)
        i += 1
    total = 0
    for a in A:
        b = format(a, "b")
        sq = 1
        for i in range(len(b)):
            if b[i] == "1":
                sq = sq * cache[len(b) - i] % p
        total += sq
    return total


class RepeatedSquaringTest(unittest.TestCase):
    def test_1(self):
        self.assertEqual(14, repeated_squaring_sum(2, [1, 2, 3]))
        self.assertEqual(253110, repeated_squaring_sum(2, [0, 100]))


def main():
    import sys
    MOD = 1000003
    if len(sys.argv) > 2:
        x = int(sys.argv[1])
        A = [int(e) for e in sys.argv[2:]]
        print(f"x: {x}, A: {A}", file=sys.stderr)
    else:
        x, N = map(int, input().split())
        A = [int(e) for e in input()]
    print(repeated_squaring_sum(x, A, MOD))


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