結果

問題 No.1092 modular arithmetic
ユーザー ShibuyapShibuyap
提出日時 2020-06-26 23:43:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 199,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 08:41:00
合計ジャッジ時間 4,435 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 43 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 46 ms
4,380 KB
testcase_04 AC 39 ms
4,380 KB
testcase_05 AC 40 ms
4,380 KB
testcase_06 AC 29 ms
4,380 KB
testcase_07 AC 39 ms
4,380 KB
testcase_08 AC 43 ms
4,380 KB
testcase_09 AC 38 ms
4,376 KB
testcase_10 AC 33 ms
4,380 KB
testcase_11 AC 35 ms
4,376 KB
testcase_12 AC 29 ms
4,376 KB
testcase_13 AC 16 ms
4,376 KB
testcase_14 AC 18 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 18 ms
4,380 KB
testcase_18 AC 47 ms
4,380 KB
testcase_19 AC 29 ms
4,380 KB
testcase_20 AC 48 ms
4,380 KB
testcase_21 AC 38 ms
4,380 KB
testcase_22 AC 45 ms
4,376 KB
testcase_23 AC 55 ms
4,376 KB
testcase_24 AC 17 ms
4,376 KB
testcase_25 AC 42 ms
4,376 KB
testcase_26 AC 54 ms
4,376 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 43 ms
4,380 KB
testcase_29 AC 64 ms
4,376 KB
testcase_30 AC 43 ms
4,380 KB
testcase_31 AC 6 ms
4,376 KB
testcase_32 AC 38 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a / b * x;
    return d;
}

inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m);
}

int main() {
    ll p, n;
    cin >> p >> n;
    ll a[n];
    rep(i,n)cin >> a[i];
    ll ans = a[0];
    string s;
    cin >> s;
    rep(i,n-1){
        if(s[i] == '+'){
            ans = (ans + a[i+1]) % p;
        }
        if(s[i] == '-'){
            ans = (ans + p - a[i+1]) % p;
        }
        if(s[i] == '*'){
            ans = ans * a[i+1] % p;
        }
        if(s[i] == '/'){
            ans = ans * modinv(a[i+1],p) % p;
        }
    }
    cout << ans << endl;
    return 0;
}
 
 

0