結果

問題 No.988 N×Mマス計算(総和)
ユーザー HaarHaar
提出日時 2020-02-16 02:36:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 3,050 bytes
コンパイル時間 1,938 ms
コンパイル使用メモリ 202,108 KB
実行使用メモリ 4,564 KB
最終ジャッジ日時 2023-09-20 12:05:16
合計ジャッジ時間 3,473 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 24 ms
4,376 KB
testcase_11 AC 35 ms
4,376 KB
testcase_12 AC 59 ms
4,380 KB
testcase_13 AC 28 ms
4,376 KB
testcase_14 AC 26 ms
4,376 KB
testcase_15 AC 24 ms
4,380 KB
testcase_16 AC 48 ms
4,376 KB
testcase_17 AC 60 ms
4,380 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 56 ms
4,376 KB
testcase_20 AC 82 ms
4,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


/**
 * @attention 使用前にstatic変数Mを設定する。
 */
class RuntimeModInt{
public:
  static std::uint32_t M;
  
  std::uint64_t val;
  RuntimeModInt(): val(0){}
  RuntimeModInt(std::int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  const auto operator+(const RuntimeModInt &a) const {return RuntimeModInt(val + a.val);}
  const auto operator-(const RuntimeModInt &a) const {return RuntimeModInt(val - a.val);}
  const auto operator*(const RuntimeModInt &a) const {return RuntimeModInt(val * a.val);}
  const auto operator/(const RuntimeModInt &a) const {return RuntimeModInt(val * a.inv().val);}
  
  const auto& operator=(const RuntimeModInt &a){val = a.val; return *this;}
  const auto& operator+=(const RuntimeModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  const auto& operator-=(const RuntimeModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  const auto& operator*=(const RuntimeModInt &a){(val *= a.val) %= M; return *this;}
  const auto& operator/=(const RuntimeModInt &a){(val *= a.inv().val) %= M; return *this;}

  const bool operator==(const RuntimeModInt &a) const {return val == a.val;}
  const bool operator!=(const RuntimeModInt &a) const {return val != a.val;}

  const static auto power(std::int64_t n, std::int64_t p){
    RuntimeModInt ret = 1, e = n;
    for(; p; e *= e, p >>= 1) if(p & 1) ret *= e;
    return ret;
  }

  const auto power(std::int64_t p) const {
    RuntimeModInt ret = 1, e = val;
    for(; p; e *= e, p >>= 1) if(p & 1) ret *= e;
    return ret;
  }
  
  const RuntimeModInt inv() const {
    std::int64_t a = val, b = M, u = 1, v = 0;

    while(b){
      std::int64_t t = a/b;
      a -= t*b; std::swap(a,b);
      u -= t*v; std::swap(u,v);
    }
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
};

auto operator-(const RuntimeModInt &a){return RuntimeModInt(-a.val);}

auto operator+(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) + b;}
auto operator-(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) - b;}
auto operator*(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) * b;}
auto operator/(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) / b;}

std::uint32_t RuntimeModInt::M;

std::istream& operator>>(std::istream &is, RuntimeModInt &a){is >> a.val; return is;}
std::ostream& operator<<(std::ostream &os, const RuntimeModInt &a){os << a.val; return os;}

using mint = RuntimeModInt;

int main(){
  int N, M, K; std::cin >> N >> M >> K;
  mint::M = K;
  char op; std::cin >> op;
  std::vector<int64_t> A(N), B(M);
  for(int i = 0; i < M; ++i) std::cin >> B[i];
  for(int i = 0; i < N; ++i) std::cin >> A[i];

  mint ans = 0;

  if(op == '+'){
    for(int i = 0; i < N; ++i) ans += A[i] * M;
    for(int i = 0; i < M; ++i) ans += B[i] * N;
  }else{
    mint sb = std::accumulate(B.begin(), B.end(), 0LL);
    for(int i = 0; i < N; ++i) ans += A[i] * sb;
  }

  std::cout << ans << std::endl;
  
  return 0;
}
0