結果

問題 No.1092 modular arithmetic
ユーザー rurun
提出日時 2023-09-01 17:49:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 713 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 194,544 KB
最終ジャッジ日時 2025-02-16 16:00:08
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;

lint modinv(lint n, lint mod=998244353) {
  lint m = mod, u = 1, v = 0;
  while (m) {
      long long t = n / m;
      n -= t * m; swap(n, m);
      u -= t * v; swap(u, v);
  }
  
  u %= mod;
  if (u < 0) u += mod;
  return u;
}

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