結果

問題 No.1092 modular arithmetic
ユーザー あるふぁ
提出日時 2026-08-30 10:57:37
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 39 ms / 2,000 ms
+ 835µs
コード長 629 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,601 ms
コンパイル使用メモリ 351,872 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-08-30 10:57:45
合計ジャッジ時間 6,180 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll modpow(ll a, ll e, ll m){
    ll r = 1;
    while (e){
        if (e&1) r = r*a%m;
        a = a*a%m;
        e >>= 1;
    }
    return r;
}

int main(){
    int P, N;
    cin >> P >> N;
    vector<int> A(N);
    string S;
    for (int& x : A) cin >> x;
    cin >> S;

    ll now = A[0];
    for (int i = 1; i < N; i++){
        if (S[i-1] == '+') now = (now+A[i])%P;
        else if (S[i-1] == '-') now = (now-A[i]+P)%P;
        else if (S[i-1] == '*') now = (now*A[i])%P;
        else now = (now*modpow(A[i], P-2, P))%P;
    }

    cout << now << endl;
}
0