結果

問題 No.2273 一点乗除区間積
ユーザー risujirohrisujiroh
提出日時 2023-04-14 23:01:48
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 5,000 ms
コード長 1,661 bytes
コンパイル時間 4,224 ms
コンパイル使用メモリ 288,808 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-18 20:37:10
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 98 ms
5,376 KB
testcase_19 AC 100 ms
10,112 KB
testcase_20 AC 261 ms
17,308 KB
testcase_21 AC 334 ms
17,384 KB
testcase_22 AC 242 ms
17,408 KB
testcase_23 AC 236 ms
17,340 KB
testcase_24 AC 247 ms
17,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/modint>
#include <atcoder/segtree>

using namespace std;

map<int, int> factor(int n) {
  map<int, int> ret;
  for (int i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ++ret[i];
      n /= i;
    }
  }
  if (1 < n) {
    ++ret[n];
  }
  return ret;
}

using atcoder::modint;

vector<int> ps;

struct S {
  modint r;
  array<int, 9> e;
};

S op(S x, S y) {
  auto e = x.e;
  for (size_t j = 0; j < size(e); ++j) {
    e[j] += y.e[j];
  }
  return {x.r * y.r, e};
}
S e() { return {1, {}}; }

S makeS(int64_t A) {
  if (A == 0) {
    return {0, {}};
  }
  auto s = e();
  for (size_t j = 0; j < size(ps); ++j) {
    while (A % ps[j] == 0) {
      ++s.e[j];
      A /= ps[j];
    }
  }
  s.r = A;
  return s;
}

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

  int N, B, Q;
  cin >> N >> B >> Q;

  modint::set_mod(B);

  for (auto [p, e] : factor(B)) {
    ps.push_back(p);
  }

  atcoder::segtree<S, op, e> seg(N);

  for (int i = 0; i < N; ++i) {
    int64_t A;
    cin >> A;

    seg.set(i, makeS(A));
  }

  while (Q--) {
    int i;
    int64_t m;
    int l, r;
    cin >> i >> m >> l >> r;
    ++r;

    auto s = makeS(m);
    if (m == B) {
      bool multiple = true;
      auto e = seg.get(i).e;
      for (size_t j = 0; j < size(ps); ++j) {
        multiple &= s.e[j] <= e[j];
      }
      if (multiple) {
        for (int& x : s.e) {
          x = -x;
        }
      }
    }
    seg.set(i, op(seg.get(i), s));
    s = seg.prod(l, r);
    auto ans = s.r;
    for (size_t j = 0; j < size(ps); ++j) {
      ans *= modint(ps[j]).pow(s.e[j]);
    }
    cout << ans.val() << '\n';
  }
}
0