結果

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

テストケース

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

ソースコード

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().split()]
    print(repeated_squaring_sum(x, A, MOD))


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