結果
| 問題 |
No.3026 Range LCM (Online Version)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-02-15 17:07:43 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 13,216 bytes |
| コンパイル時間 | 5,975 ms |
| コンパイル使用メモリ | 333,776 KB |
| 実行使用メモリ | 814,584 KB |
| 最終ジャッジ日時 | 2025-02-15 17:08:02 |
| 合計ジャッジ時間 | 17,391 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 MLE * 1 -- * 25 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); }
typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define FOR(i,l,r) for (int i = l;i < (int)(r); i++)
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const ll MOD = 998244353LL;
const int MOD2 = 1e9+7;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};
#ifdef LOCAL
# include <debug_print.hpp>
# define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
# define debug(...) (static_cast<void>(0))
#endif
using mint = modint998244353;
// https://qiita.com/drken/items/3beb679e54266f20ab63
struct Eratosthenes{
int N;
vector<bool> isprime;
vector<int> minfactor, mobius;
Eratosthenes(int N_max = 1e7){init(N_max);}
//初期化
void init(int N_max = 1e7){
int N = N_max;
isprime.assign(N+1,true);
minfactor.assign(N+1,-1);
mobius.assign(N+1,1);
//Eratosthenes O(NloglogN)
isprime[0] = false;
isprime[1] = false;
for(int p=2;p<=N;p++){
if (!isprime[p])continue;
minfactor[p] = p;
mobius[p] = -1;
//pの倍数の処理
for(int q=2*p;q<=N;q+=p){
isprime[q] = false;
if (minfactor[q] == -1){
minfactor[q] = p;
}
if ((q/p) % p == 0) mobius[q] = 0;
else mobius[q] *= -1;
}
}
}
//素数判定,O(1)
bool judge_prime(int num){
return isprime[num];
}
//素数列挙,O(N)
vector<int> list_primes(int num = -1){
if (num == -1) num = N;
vector<int> primes;
for(int p=0;p<=num;p++){
if (isprime[p]) primes.push_back(p);
}
return primes;
}
//高速素因数分解,O(logN),{(素因数,個数)...}
vector<pii> factorize(int x){
vector<pii> ans;
while(x > 1){
int p = minfactor[x];
int e = 0;
while(minfactor[x] == p){
x /= p;
e++;
}
ans.push_back(make_pair(p,e));
}
return ans;
}
//高速約数列挙 O(240(N <= 1e6),1344(N <= 1e9))
vector<int> divisors(int x){
vector<int> ans; ans.push_back(1);
vector<pii> facts = factorize(x);
for(auto [p,e]:facts){
int s = ans.size();
for(int i=0;i<s;i++){
int v = 1;
for(int j=0;j<e;j++){
v *= p;
ans.push_back(ans[i] * v);
}
}
}
sort(all(ans));
return ans;
}
//オイラーのphi関数
ll euler_phi(int x){
auto facts = factorize(x);
ll res = x;
for(auto [p, e]:facts){
res = res - res / p;
}
return res;
}
//メビウス関数
//mobius[1] = 1
//nが素数pで2回以上割り切れる -> mobius[n] = 0
//mobius[n] = pow(-1,Nの素数の種類)
int my_mobius(int x){
return mobius[x];
}
};
// f -> F, 累積和Fを求める
template<typename T>
vector<T> fast_zeta(vector<T> &f){
vector<T> res = f;
int N = f.size() - 1;
Eratosthenes er(N);
for(int p=2;p<=N;p++){
if (!er.judge_prime(p)) continue;
for(int k=(N/p);k>0;k--){
res[k] += res[k * p];
}
}
return res;
}
// F -> f, 累積和Fを分解する
template<typename T>
vector<T> fast_mobius(vector<T> &F){
vector<T> res = F;
int N = F.size() - 1;
Eratosthenes er(N);
for(int p=2;p<=N;p++){
if (!er.judge_prime(p)) continue;
for(int k=1;k<=(N/p);k++){
res[k] -= res[k * p];
}
}
return res;
}
template<typename T>
vector<T> gcd_conv(vector<T> &f, vector<T> &g){
int N = max(f.size(), g.size());
vector<T> F(N+1) , G(N+1), H(N+1);
for(int i=0;i<f.size();i++){
F[i] = f[i];
}
for(int i=0;i<g.size();i++){
G[i] = g[i];
}
F = fast_zeta(F);
G = fast_zeta(G);
for(int i=1;i<=N;i++){
H[i] = F[i] * G[i];
}
H = fast_mobius(H);
return H;
}
// https://judge.yosupo.jp/submission/214728
//【完全永続セグメント木(モノイド)】
/*
* Persistent_segtree<S, op, e>(int n) : O(n)
* v[0..n) = e() で初期化する.
* 要素はモノイド (S, op, e) の元とする.
*
* Persistent_segtree<S, op, e>(vS v) : O(n)
* 配列 v[0..n) の要素で初期化する.
*
* int set(int i, S x, int t) : O(log n)
* t 番目の履歴に対し v[i] = x とした配列を最新の履歴として記録し,履歴番号を返す.
*
* S get(int i, int t) : O(log n)
* t 番目の履歴の v[i] を返す.
*
* S prod(int l, int r, int t) : O(log n)
* t 番目の履歴の Πv[l..r) を返す.
*
* S all_prod(int t) : O(1)
* t 番目の履歴の Πv[0..n) を返す.
*
* int max_right(int l, function<bool(S)> f, int t) : O(log n)
* t 番目の履歴について,f(Πv[l..r)) = true となる最大の r を返す.
* 制約:f(e()) = true,f は単調
*
* int min_left(int r, function<bool(S)> f, int t) : O(log n)
* t 番目の履歴について,f(Πv[l..r)) = true となる最小の l を返す.
* 制約:f(e()) = true,f は単調
*/
template <class S, S(*op)(S, S), S(*e)()>
class Persistent_segtree {
struct Node {
int l, r;
S val; // Πv[l..r) の値
Node* lp, * rp; // 左右の子へのポインタ
Node(int l_, int r_, S val_ = e(), Node* lp_ = nullptr, Node* rp_ = nullptr)
: l(l_), r(r_), val(val_), lp(lp_), rp(rp_) {}
};
int n; // 配列の大きさ
int T; // 履歴の個数
vector<Node*> his; // 履歴へのポインタ
Node* init_rf(const vector<S>& v, int l, int r) {
// 葉を作る場合
if (r - l == 1) {
Node* p = new Node(l, r, v[l]);
return p;
}
Node* p = new Node(l, r);
int m = (l + r) / 2;
p->lp = init_rf(v, l, m);
p->rp = init_rf(v, m, r);
p->val = op(p->lp->val, p->rp->val);
return p;
}
Node* set_rf(Node* p, int i, S x) {
// p が葉の場合
if (p->r - p->l == 1) {
Node* np = new Node(p->l, p->r, x);
return np;
}
Node* np = new Node(p->l, p->r);
int m = (p->l + p->r) / 2;
if (i < m) {
np->lp = set_rf(p->lp, i, x);
np->rp = p->rp;
}
else {
np->lp = p->lp;
np->rp = set_rf(p->rp, i, x);
}
np->val = op(np->lp->val, np->rp->val);
return np;
}
S get_rf(Node* p, int i) const {
// p が葉の場合
if (p->r - p->l == 1) return p->val;
int m = (p->l + p->r) / 2;
if (i < m) return get_rf(p->lp, i);
else return get_rf(p->rp, i);
}
S prod_rf(Node* p, int l, int r) const {
// 範囲外なら単位元 e() を返す.
if (p->r <= l || r <= p->l) return e();
// 完全に範囲内なら葉まで降りず自身の値を返す.
if (l <= p->l && p->r <= r) return p->val;
// 一部の範囲のみを含むなら子を見に行く.
S vl = prod_rf(p->lp, l, r);
S vr = prod_rf(p->rp, l, r);
return op(vl, vr);
}
int max_right_rf(Node* p, int l, S& x, const function<bool(S)>& f) const {
// 範囲外の場合
if (p->r <= l) return n;
// f( Πv[p->l..p->r) ) = true の場合
if (f(op(x, p->val))) {
x = op(x, p->val);
return n;
}
// p が葉の場合,これがちょうど条件を満たさなくなる値なのでその位置を返す.
if (p->r - p->l == 1) return p->l;
// 左の部分木から見に行く.境界が見つかったらそれを返す.
int pos = max_right_rf(p->lp, l, x, f);
if (pos != n) return pos;
// 見つからなかったら右の部分木も見に行き,結果を返す.
return max_right_rf(p->rp, l, x, f);
}
int min_left_rf(Node* p, int r, S& x, const function<bool(S)>& f) const {
// 範囲外の場合
if (r <= p->l) return -1;
// f( Πv[p->l..p->r) ) = true の場合
if (f(op(p->val, x))) {
x = op(p->val, x);
return -1;
}
// p が葉の場合,これがちょうど条件を満たさなくなる値なのでその位置を返す.
if (p->r - p->l == 1) return p->l;
// 右の部分木から見に行く.境界が見つかったらそれを返す.
int pos = min_left_rf(p->rp, r, x, f);
if (pos != -1) return pos;
// 見つからなかったら左の部分木も見に行き,結果を返す.
return min_left_rf(p->lp, r, x, f);
}
void print_rf(Node* p, ostream& os) const {
if (p->r - p->l == 1) {
os << p->val << " ";
return;
}
print_rf(p->lp, os);
print_rf(p->rp, os);
}
public:
// 配列 v[0..n) の要素で初期化する.
Persistent_segtree(const vector<S>& v) : n(int(v.size())), T(1), his(1) {
his[0] = init_rf(v, 0, n);
}
// v[0..n) = e() で初期化する.
Persistent_segtree(int n_) : n(n_), T(1), his(1) {
// verify : https://atcoder.jp/contests/abc165/tasks/abc165_f
vector<S> v(n, e());
his[0] = init_rf(v, 0, n);
}
Persistent_segtree() : n(0), T(0) {} // ダミー
// t 番目の履歴に対し v[i] = x とした配列を最新の履歴として記録し,履歴番号を返す.
int set(int i, S x, int t) {
// verify : https://atcoder.jp/contests/abc165/tasks/abc165_f
assert(0 <= i && i < n);
assert(t < T);
his.push_back(set_rf(his[t], i, x));
return T++;
}
// t 番目の履歴の v[i] を返す.
S get(int i, int t) const {
assert(0 <= i && i < n);
assert(t < T);
return get_rf(his[t], i);
}
// t 番目の履歴の Πv[l..r) を返す.
S prod(int l, int r, int t) const {
// verify : https://atcoder.jp/contests/abc165/tasks/abc165_f
assert(0 <= l && r <= n);
assert(t < T);
if (l >= r) return e();
return prod_rf(his[t], l, r);
}
// t 番目の履歴の Πv[0..n) を返す.
S all_prod(int t) const {
// verify : https://atcoder.jp/contests/abc165/tasks/abc165_f
assert(t < T);
return prod(0, n, t);
}
// t 番目の履歴について,f(Πv[l..r)) = true となる最大の r を返す.
int max_right(int l, const function<bool(S)>& f, int t) const {
// verify : https://atcoder.jp/contests/practice2/tasks/practice2_j
S x(e());
return max_right_rf(his[t], l, x, f);
}
// t 番目の履歴について,f(Πv[l..r)) = true となる最小の l を返す.
int min_left(int r, const function<bool(S)>& f, int t) const {
S x(e());
return min_left_rf(his[t], r, x, f);
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Persistent_segtree& seg) {
rep(t, seg.T) {
os << t << ": ";
seg.print_rf(seg.his[t], os);
os << endl;
}
return os;
}
#endif
};
// 初期ではT=0
using S = mint;
S op(S a, S b){ return a * b; }
S e(){ return 1; }
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
ll N; cin >> N;
vector<int> A(N);
rep(i, N) cin >> A[i];
int MA = *max_element(all(A));
Eratosthenes er(MA);
Persistent_segtree<S, op, e> seg(N);
vector<mint> B(N, 1);
vector pw(MA + 1, vector<mint>{1});
vector stk(MA + 1, stack<pii>{});
rep(p, MA + 1) stk[p].push(make_pair(INF, -1));
int version = 0;
map<int, int> node;
node[0] = version;
rep(i, N){
debug(i);
for(auto [p, e] : er.factorize(A[i])){
while((int)pw[p].size() <= e) pw[p].push_back(pw[p].back() * (mint)p);
int base = 0;
while(stk[p].top().first + base <= e){
auto [pj, j] = stk[p].top();
stk[p].pop();
B[j] /= pw[p][pj];
base += pj;
version = seg.set(j, B[j], version);
}
if(stk[p].top().second != -1){
auto &[pj, j] = stk[p].top();
pj -= (e - base);
B[j] /= pw[p][e - base];
version = seg.set(j, B[j], version);
}
stk[p].push({e, i});
B[i] *= pw[p][e];
version = seg.set(i, B[i], version);
}
node[i + 1] = version;
}
int Q; cin >> Q;
vector<ll> ans(Q + 1, 1);
FOR(q, 1, Q + 1){
ll a, b;
cin >> a >> b;
ll x = a * ans[q - 1] % MOD;
ll y = x % N + 1;
ll z = b * ans[q - 1] % MOD;
ll w = z % N + 1;
ll L = min(y, w), R = max(y, w);
L--;
ans[q] = seg.prod(L, R, node[R]).val();
}
FOR(q, 1, Q + 1) cout << ans[q] << "\n";
}