結果

問題 No.1092 modular arithmetic
ユーザー SSRSSSRS
提出日時 2021-12-19 19:57:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 801 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 166,740 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-13 18:58:09
合計ジャッジ時間 5,871 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 39 ms
4,376 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 42 ms
4,368 KB
testcase_04 AC 36 ms
4,372 KB
testcase_05 AC 38 ms
4,368 KB
testcase_06 AC 28 ms
4,372 KB
testcase_07 AC 35 ms
4,372 KB
testcase_08 AC 40 ms
4,368 KB
testcase_09 AC 36 ms
4,368 KB
testcase_10 AC 31 ms
4,372 KB
testcase_11 AC 34 ms
4,372 KB
testcase_12 AC 27 ms
4,368 KB
testcase_13 AC 11 ms
4,368 KB
testcase_14 AC 14 ms
4,368 KB
testcase_15 AC 4 ms
4,368 KB
testcase_16 AC 10 ms
4,372 KB
testcase_17 AC 14 ms
4,372 KB
testcase_18 AC 43 ms
4,368 KB
testcase_19 AC 26 ms
4,372 KB
testcase_20 AC 44 ms
4,372 KB
testcase_21 AC 35 ms
4,368 KB
testcase_22 AC 42 ms
4,368 KB
testcase_23 AC 59 ms
4,372 KB
testcase_24 AC 18 ms
4,368 KB
testcase_25 AC 45 ms
4,372 KB
testcase_26 AC 56 ms
4,372 KB
testcase_27 AC 9 ms
4,372 KB
testcase_28 AC 45 ms
4,368 KB
testcase_29 AC 70 ms
4,368 KB
testcase_30 AC 45 ms
4,372 KB
testcase_31 AC 7 ms
4,368 KB
testcase_32 AC 41 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long MOD){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
long long modinv(long long a, long long MOD){
  return modpow(a, MOD - 2, MOD);
}
int main(){
  int P, N;
  cin >> P >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  string S;
  cin >> S;
  long long ans = A[0];
  for (int i = 0; i < N - 1; i++){
    if (S[i] == '+'){
      ans += A[i + 1];
    }
    if (S[i] == '-'){
      ans += P - A[i + 1];
    }
    if (S[i] == '*'){
      ans *= A[i + 1];
    }
    if (S[i] == '/'){
      ans *= modinv(A[i + 1], P);
    }
    ans %= P;
  }
  cout << ans << endl;
}
0