結果

問題 No.1092 modular arithmetic
ユーザー CELICA
提出日時 2020-08-14 10:31:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 904 bytes
コンパイル時間 1,940 ms
コンパイル使用メモリ 170,196 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-10 11:33:16
合計ジャッジ時間 5,040 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll modinv(ll a, ll m)
{
    ll b{ m }, u{ 1 }, v{ 0 };
    while (b)
    {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

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

    long p, n;
    cin >> p >> n;

    vector<long> v(n);
    for (auto&& iv : v)
        cin >> iv;

    string s;
    cin >> s;

    ll res{ v[0] };
    for (int i = 0; i < n - 1; ++i)
    {
        if (s[i] == '+')
            res += v[i + 1];
        else if (s[i] == '-')
            res -= v[i + 1];
        else if (s[i] == '*')
            res *= v[i + 1];
        else if (s[i] == '/')
            res *= modinv(v[i + 1], p);
        res %= p;
    }

    if (res < 0)
        res += p;

    cout << res << "\n";

    return 0;
}
0