結果

問題 No.1092 modular arithmetic
ユーザー lanegue
提出日時 2020-06-26 00:50:31
言語 Crystal
(1.14.0)
結果
AC  
実行時間 342 ms / 2,000 ms
コード長 562 bytes
コンパイル時間 12,153 ms
コンパイル使用メモリ 302,144 KB
実行使用メモリ 11,860 KB
最終ジャッジ日時 2024-06-30 20:19:46
合計ジャッジ時間 17,268 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

require "big"
pr, n = read_line.split.map{|i|i.to_i64}
a = read_line.split.map{|i|i.to_i64}
s = read_line
r = a[0].to_i64
def pow_mod(a, n, m)
    r = BigInt.new(1)
    a = BigInt.new(a)
    while n > 0
        if n % 2 == 1
            r *= a
            r %= m
        end
        a **= 2
        a %= m
        n >>= 1
    end
    r
end
(n - 1).times{|i|
    case s[i]
    when '+'
        r += a[i + 1]
    when '-'
        r -= a[i + 1]
    when '*'
        r *= a[i + 1]
    when '/'
        r *= pow_mod(a[i + 1], pr - 2, pr)
    end
    r %= pr
}
puts r
0