結果

問題 No.3026 Range LCM (Online Version)
ユーザー 👑 hos.lyric
提出日時 2025-02-14 23:03:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 10,104 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 140,064 KB
実行使用メモリ 203,444 KB
最終ジャッジ日時 2025-02-14 23:04:55
合計ジャッジ時間 63,407 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33 TLE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:233:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  233 |       scanf("%d", &A[i]);
      |       ~~~~~^~~~~~~~~~~~~
main.cpp:235:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  235 |     scanf("%d", &Q);
      |     ~~~~~^~~~~~~~~~
main.cpp:276:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  276 |       scanf("%d%d", &L, &R);
      |       ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

// M = 2^K N + 1
// init: O(2^(K/2) + N), query: O(log(M))
// g must be a primitive root
template <unsigned M_> struct ModLog {
  static constexpr unsigned M = M_;
  static constexpr int K = __builtin_ctz(M - 1);
  static constexpr int N = (M - 1) >> K;
  static_assert(M == (N << K) + 1, "[ModLog] M = 2^K N + 1 must hold.");
  const ModInt<M> g;
  // N^-1 mod 2^K
  unsigned invN;
  // (g^N)^(-2^*)
  ModInt<M> hs[K];
  // ((g^N)^(2^floor(K/2)))^*, (g^(2^K))^*
  pair<unsigned, int> ps[1 << (K-K/2)], qs[N];
  ModLog() {}
  explicit ModLog(ModInt<M> g_) : g(g_) {
    invN = N;
    for (int i = 0; i < 4; ++i) invN *= (2 - N * invN);
    invN &= ((1 << K) - 1);
    hs[0] = g.pow(-N);
    for (int k = 1; k < K; ++k) hs[k] = hs[k - 1] * hs[k - 1];
    const ModInt<M> p = g.pow(N << (K/2));
    ModInt<M> pp = 1;
    for (int x = 0; x < 1 << (K-K/2); ++x) { ps[x] = std::make_pair(pp.x, x); pp *= p; }
    std::sort(ps, ps + (1 << (K-K/2)));
    const ModInt<M> q = g.pow(1 << K);
    ModInt<M> qq = 1;
    for (int y = 0; y < N; ++y) { qs[y] = std::make_pair(qq.x, y); qq *= q; }
    std::sort(qs, qs + N);
  }
  int operator()(ModInt<M> a) const {
    assert(a);
    // mod 2^K
    ModInt<M> b = a.pow(N);
    ModInt<M> bb = b;
    for (int k = 0; k < K/2; ++k) bb *= bb;
    int x = std::partition_point(ps, ps + (1 << (K-K/2)), [&](const pair<unsigned, int> &p) -> bool {
      return (p.first < bb.x);
    })->second;
    for (int k = 0; k < K-K/2; ++k) if (x >> k & 1) b *= hs[k];
    x |= (std::partition_point(ps, ps + (1 << (K-K/2)), [&](const pair<unsigned, int> &p) -> bool {
      return (p.first < b.x);
    })->second) << (K/2);
    // mod N
    ModInt<M> c = a;
    for (int k = 0; k < K; ++k) c *= c;
    const int y = std::partition_point(qs, qs + N, [&](const pair<unsigned, int> &q) -> bool {
      return (q.first < c.x);
    })->second;
    return y + N * static_cast<int>((static_cast<long long>(invN) * (x - y)) & ((1 << K) - 1));
  }
};

////////////////////////////////////////////////////////////////////////////////


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}

// point add, rectangle sum
template <class X, class Y, class T> struct Bit2d {
  vector<X> xs;
  vector<pair<Y, X>> yxs;
  vector<vector<Y>> yss;
  int m;
  vector<int> ns;
  vector<vector<T>> bit;
  Bit2d() {}
  void book(X x, Y y) {
    xs.push_back(x);
    yxs.emplace_back(y, x);
  }
  void build() {
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    m = xs.size();
    yss.assign(m, {});
    sort(yxs.begin(), yxs.end());
    for (const auto &yx : yxs) {
      const X x = yx.second;
      const Y y = yx.first;
      const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
      assert(a < m); assert(xs[a] == x);
      for (int u = a; u < m; u |= u + 1) yss[u].push_back(y);
    }
    ns.assign(m, 0);
    bit.assign(m, {});
    for (int u = 0; u < m; ++u) {
      yss[u].erase(unique(yss[u].begin(), yss[u].end()), yss[u].end());
      ns[u] = yss[u].size();
      bit[u].assign(ns[u], 0);
    }
  }
  void add(X x, Y y, T val) {
    const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    assert(a < m); assert(xs[a] == x);
    for (int u = a; u < m; u |= u + 1) {
      const int b = lower_bound(yss[u].begin(), yss[u].end(), y) - yss[u].begin();
      assert(b < ns[u]); assert(yss[u][b] == y);
      for (int v = b; v < ns[u]; v |= v + 1) bit[u][v] += val;
    }
  }
  T get(X x0, X x1, Y y0, Y y1) const {
    const int a0 = lower_bound(xs.begin(), xs.end(), x0) - xs.begin();
    const int a1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();
    T ret = 0;
    for (int u = a0; u; u &= u - 1) ret -= get(u - 1, y0, y1);
    for (int u = a1; u; u &= u - 1) ret += get(u - 1, y0, y1);
    return ret;
  }
 private:
  T get(int u, Y y0, Y y1) const {
    T ret = 0;
    const int b0 = lower_bound(yss[u].begin(), yss[u].end(), y0) - yss[u].begin();
    const int b1 = lower_bound(yss[u].begin(), yss[u].end(), y1) - yss[u].begin();
    for (int v = b0; v; v &= v - 1) ret -= bit[u][v - 1];
    for (int v = b1; v; v &= v - 1) ret += bit[u][v - 1];
    return ret;
  }
};


constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
using Mint1 = ModInt<MO - 1>;

const ModLog<MO> modLog(3);

constexpr int M = 200'010;
vector<int> lpf;

int N, Q;
vector<int> A;

Mint whole[450][200'010];

int main() {
  lpf.assign(M + 1, 0);
  for (int p = 2; p <= M; ++p) lpf[p] = p;
  for (int p = 2; p <= M; ++p) if (lpf[p] == p) {
    for (int n = p; n <= M; n += p) chmin(lpf[n], p);
  }
  
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &A[i]);
    }
    scanf("%d", &Q);
    
    vector<pair<
      pair<int, int>,
      int
    >> events;
    vector<int> app(M + 1, N);
    for (int i = N; --i >= 0; ) {
      for (int a = A[i]; a > 1; ) {
        const int p = lpf[a];
        int e = 0;
        do { ++e; a /= p; } while (a % p == 0);
        int q = 1;
        for (int f = 1; f <= e; ++f) {
          q *= p;
          // l=i+1 ~~> l=i; *= p for i<r<=app[q]
          events.emplace_back(make_pair(i, app[q]), p);
          app[q] = i;
        }
      }
    }
    
    vector<Mint1> bit1(N, 0);
    Bit2d<int, int, Mint1> bit2;
    for (const auto &e : events) {
      const int l = e.first.first;
      const int r = e.first.second;
      bit2.book(l, r);
    }
    bit2.build();
    for (const auto &e : events) {
      const int l = e.first.first;
      const int r = e.first.second;
      const Mint1 val = modLog(e.second);
      bAdd(bit1, l, +val);
      bit2.add(l, r, -val);
    }
    
    Mint ans = 1;
    for (int q = 0; q < Q; ++q) {
      int L, R;
      scanf("%d%d", &L, &R);
      L = (int)(L * ans).x % N + 1;
      R = (int)(R * ans).x % N + 1;
      if (L > R) swap(L, R);
      --L;
// cerr<<COLOR("93")<<L<<" "<<R<<COLOR()<<endl;
      const Mint1 res = bSum(bit1, L, R) + bit2.get(L, N, L, R);
      ans = modLog.g.pow(res.x);
      printf("%u\n", ans.x);
    }
  }
  return 0;
}
0