結果

問題 No.1092 modular arithmetic
ユーザー Hydrogen332
提出日時 2024-11-16 14:51:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 924 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 247,608 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-16 14:51:47
合計ジャッジ時間 8,094 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)
#define all(a) (a).begin(),(a).end()

ll modpow(ll a, ll n, ll m) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % m;
        a = a * a % m;
        n >>= 1;
    }
    return res;
}

ll modinv(ll a, ll m) {
    return modpow(a, m - 2, m);
}

int main() {
    cin.tie(nullptr);
    
    ll P, N;
    cin >> P >> N;
    vector<ll> A(N);
    rep(i, 0, N) cin >> A[i];
    string S;
    cin >> S;
    
    ll ans = A[0];
    rep(i, 0, N - 1) {
        if (S[i] == '+') {
            ans = (ans + A[i + 1]) % P;
        } else if (S[i] == '-') {
            ans = (ans + 2 * P - A[i + 1]) % P;
        } else if (S[i] == '*') {
            ans = (ans * A[i + 1]) % P;
        } else {
            ans = (ans * modinv(A[i + 1], P)) % P;
        }
    }
    cout << ans << '\n';
}
0