結果

問題 No.1092 modular arithmetic
ユーザー fine
提出日時 2020-06-26 23:51:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 932 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 168,844 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 00:52:11
合計ジャッジ時間 3,558 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

constexpr ll MOD = 1000000007;

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll p;
    int n;
    cin >> p >> n;

    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    string s;
    cin >> s;

    ll ans = a.front();
    for (int i = 0; i < n - 1; i++) {
        if (s[i] == '+') {
            (ans += a[i + 1]) %= p;
        } else if (s[i] == '-') {
            (ans += p - a[i + 1]) %= p;
        } else if (s[i] == '*') {
            (ans *= a[i + 1]) %= p;
        } else {
            (ans *= modpow(a[i + 1], p - 2, p)) %= p;
        }
    }
    cout << ans << newl;

    return 0;
}
0