結果

問題 No.16 累乗の加算
ユーザー kutsutama
提出日時 2018-03-11 10:44:53
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 569 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-06-26 05:26:49
合計ジャッジ時間 1,224 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

x, n = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
mod = 10**6 + 3

def powmod(ai, mod, memo):
    if ai in memo:
        return memo[ai]
    res = 0
    if ai == 0:
        res = 1
    elif ai % 2 == 0:
        pw = powmod(ai // 2, mod, memo)
        res = pw * pw % mod
    else:
        pw = powmod((ai - 1) // 2, mod, memo)
        res = x * pw * pw % mod
    if len(memo) < 10000000:
        memo[ai] = res
    return res

memo = {}
res = 0
for ai in a:
    res = (res + powmod(ai, mod, memo)) % mod
print(res)
0