結果
| 問題 |
No.3045 反復重み付き累積和
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-02-28 23:20:51 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 871 ms / 5,000 ms |
| コード長 | 19,455 bytes |
| コンパイル時間 | 3,503 ms |
| コンパイル使用メモリ | 292,608 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2025-02-28 23:21:05 |
| 合計ジャッジ時間 | 11,781 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
// #include <bits/allocator.h> // Temp fix for gcc13 global pragma
// #pragma GCC target("avx2,bmi2,popcnt,lzcnt")
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
#ifdef LOCAL
#include "Debug.h"
#else
#define debug_endl() 42
#define debug(...) 42
#define debug2(...) 42
#define debugbin(...) 42
#endif
template<class data_t, data_t _mod>
struct modular_fixed_base{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>)
static_assert(IS_UNSIGNED(data_t));
static_assert(1 <= _mod && _mod < data_t(1) << 8 * sizeof(data_t) - 1);
static constexpr bool VARIATE_MOD_FLAG = false;
static constexpr data_t mod(){
return _mod;
}
template<class T>
static vector<modular_fixed_base> precalc_power(T base, int SZ){
vector<modular_fixed_base> res(SZ + 1, 1);
for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base;
return res;
}
template<class T>
static vector<modular_fixed_base> precalc_geometric_sum(T base, int SZ){
vector<modular_fixed_base> res(SZ + 1);
for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base + base;
return res;
}
static vector<modular_fixed_base> _INV;
static void precalc_inverse(int SZ){
if(_INV.empty()) _INV.assign(2, 1);
for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]);
}
// _mod must be a prime
static modular_fixed_base _primitive_root;
static modular_fixed_base primitive_root(){
if(_primitive_root) return _primitive_root;
if(_mod == 2) return _primitive_root = 1;
if(_mod == 998244353) return _primitive_root = 3;
data_t divs[20] = {};
divs[0] = 2;
int cnt = 1;
data_t x = (_mod - 1) / 2;
while(x % 2 == 0) x /= 2;
for(auto i = 3; 1LL * i * i <= x; i += 2){
if(x % i == 0){
divs[cnt ++] = i;
while(x % i == 0) x /= i;
}
}
if(x > 1) divs[cnt ++] = x;
for(auto g = 2; ; ++ g){
bool ok = true;
for(auto i = 0; i < cnt; ++ i){
if(modular_fixed_base(g).power((_mod - 1) / divs[i]) == 1){
ok = false;
break;
}
}
if(ok) return _primitive_root = g;
}
}
constexpr modular_fixed_base(){ }
modular_fixed_base(const double &x){ data = _normalize(llround(x)); }
modular_fixed_base(const long double &x){ data = _normalize(llround(x)); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base(const T &x){ data = _normalize(x); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){
int sign = x >= 0 ? 1 : -1;
data_t v = _mod <= sign * x ? sign * x % _mod : sign * x;
if(sign == -1 && v) v = _mod - v;
return v;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data; }
modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; }
modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); }
modular_fixed_base &operator++(){ return *this += 1; }
modular_fixed_base &operator--(){ return *this += _mod - 1; }
modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; }
modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; }
modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); }
modular_fixed_base &operator*=(const modular_fixed_base &rhs){
if constexpr(is_same_v<data_t, unsigned int>) data = (unsigned long long)data * rhs.data % _mod;
else if constexpr(is_same_v<data_t, unsigned long long>){
long long res = data * rhs.data - _mod * (unsigned long long)(1.L / _mod * data * rhs.data);
data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod);
}
else data = _normalize(data * rhs.data);
return *this;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base &inplace_power(T e){
if(e == 0) return *this = 1;
if(data == 0) return *this = {};
if(data == 1 || e == 1) return *this;
if(data == mod() - 1) return e % 2 ? *this : *this = -*this;
if(e < 0) *this = 1 / *this, e = -e;
if(e == 1) return *this;
modular_fixed_base res = 1;
for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
return *this = res;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base power(T e) const{
return modular_fixed_base(*this).inplace_power(e);
}
// c + c * x + ... + c * x^{e-1}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base &inplace_geometric_sum(T e, modular_fixed_base c = 1){
if(e == 0) return *this = {};
if(data == 0) return *this = {};
if(data == 1) return *this = c * e;
if(e == 1) return *this = c;
if(data == mod() - 1) return *this = c * abs(e % 2);
modular_fixed_base res = 0;
if(e < 0) return *this = geometric_sum(-e + 1, -*this) - 1;
if(e == 1) return *this = c * *this;
for(; e; c *= 1 + *this, *this *= *this, e >>= 1) if(e & 1) res += c, c *= *this;
return *this = res;
}
// c + c * x + ... + c * x^{e-1}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base geometric_sum(T e, modular_fixed_base c = 1) const{
return modular_fixed_base(*this).inplace_geometric_sum(e, c);
}
modular_fixed_base &operator/=(const modular_fixed_base &otr){
make_signed_t<data_t> a = otr.data, m = _mod, u = 0, v = 1;
if(a < _INV.size()) return *this *= _INV[a];
while(a){
make_signed_t<data_t> t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return *this *= u;
}
#define ARITHMETIC_OP(op, apply_op)\
modular_fixed_base operator op(const modular_fixed_base &x) const{ return modular_fixed_base(*this) apply_op x; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
modular_fixed_base operator op(const T &x) const{ return modular_fixed_base(*this) apply_op modular_fixed_base(x); }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend modular_fixed_base operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x) apply_op y; }
ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=)
#undef ARITHMETIC_OP
#define COMPARE_OP(op)\
bool operator op(const modular_fixed_base &x) const{ return data op x.data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
bool operator op(const T &x) const{ return data op modular_fixed_base(x).data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend bool operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x).data op y.data; }
COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
friend istream &operator>>(istream &in, modular_fixed_base &number){
long long x;
in >> x;
number.data = modular_fixed_base::_normalize(x);
return in;
}
friend ostream &operator<<(ostream &out, const modular_fixed_base &number){
out << number.data;
#ifdef LOCAL
cerr << "(";
for(auto d = 1; ; ++ d){
if((number * d).data <= 1000000){
cerr << (number * d).data;
if(d != 1) cerr << "/" << d;
break;
}
else if((-number * d).data <= 1000000){
cerr << "-" << (-number * d).data;
if(d != 1) cerr << "/" << d;
break;
}
}
cerr << ")";
#endif
return out;
}
data_t data = 0;
#undef IS_INTEGRAL
#undef IS_UNSIGNED
};
template<class data_t, data_t _mod> vector<modular_fixed_base<data_t, _mod>> modular_fixed_base<data_t, _mod>::_INV;
template<class data_t, data_t _mod> modular_fixed_base<data_t, _mod> modular_fixed_base<data_t, _mod>::_primitive_root;
const unsigned int mod = (119 << 23) + 1; // 998244353
// const unsigned int mod = 1e9 + 7; // 1000000007
// const unsigned int mod = 1e9 + 9; // 1000000009
// const unsigned long long mod = (unsigned long long)1e18 + 9;
using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>;
modular operator""_m(const char *x){ return stoll(x); }
template<class T>
struct combinatorics{
#ifdef LOCAL
#define ASSERT(c) assert(c)
#else
#define ASSERT(c) 42
#endif
// O(n)
static vector<T> precalc_fact(int n){
vector<T> f(n + 1, 1);
for(auto i = 1; i <= n; ++ i) f[i] = f[i - 1] * i;
return f;
}
// O(n * m)
static vector<vector<T>> precalc_C(int n, int m){
vector<vector<T>> c(n + 1, vector<T>(m + 1));
for(auto i = 0; i <= n; ++ i) for(auto j = 0; j <= min(i, m); ++ j) c[i][j] = i && j ? c[i - 1][j - 1] + c[i - 1][j] : T(1);
return c;
}
int SZ = 0;
vector<T> inv, fact, invfact;
combinatorics(){ }
// O(SZ)
combinatorics(int SZ): SZ(SZ), inv(SZ + 1, 1), fact(SZ + 1, 1), invfact(SZ + 1, 1){
for(auto i = 1; i <= SZ; ++ i) fact[i] = fact[i - 1] * i;
invfact[SZ] = 1 / fact[SZ];
for(auto i = SZ - 1; i >= 0; -- i){
invfact[i] = invfact[i + 1] * (i + 1);
inv[i + 1] = invfact[i + 1] * fact[i];
}
}
// O(1)
T C(int n, int k) const{
ASSERT(0 <= min(n, k) && max(n, k) <= SZ);
return n >= k ? fact[n] * invfact[k] * invfact[n - k] : T{0};
}
// O(1)
T P(int n, int k) const{
ASSERT(0 <= min(n, k) && max(n, k) <= SZ);
return n >= k ? fact[n] * invfact[n - k] : T{0};
}
// O(1)
T H(int n, int k) const{
ASSERT(0 <= min(n, k));
if(n == 0) return 0;
return C(n + k - 1, k);
}
// Multinomial Coefficient
T mC(int n, const vector<int> &a) const{
ASSERT((int)a.size() >= 2 && accumulate(a.begin(), a.end(), 0) == n);
ASSERT(0 <= min(n, *min_element(a.begin(), a.end())) && max(n, *max_element(a.begin(), a.end())) <= SZ);
T res = fact[n];
for(auto x: a) res *= invfact[x];
return res;
}
// Multinomial Coefficient
template<class... U, typename enable_if<(is_integral_v<U> && ...)>::type* = nullptr>
T mC(int n, U... pack){
ASSERT(sizeof...(pack) >= 2 && (... + pack) == n);
return (fact[n] * ... * invfact[pack]);
}
// O(min(k, n - k))
T naive_C(long long n, long long k) const{
ASSERT(0 <= min(n, k));
if(n < k) return 0;
T res = 1;
k = min(k, n - k);
ASSERT(k <= SZ);
for(auto i = n; i > n - k; -- i) res *= i;
return res * invfact[k];
}
// O(k)
T naive_P(long long n, int k) const{
ASSERT(0 <= min<long long>(n, k));
if(n < k) return 0;
T res = 1;
for(auto i = n; i > n - k; -- i) res *= i;
return res;
}
// O(k)
T naive_H(long long n, int k) const{
ASSERT(0 <= min<long long>(n, k));
return naive_C(n + k - 1, k);
}
// O(1)
bool parity_C(long long n, long long k) const{
ASSERT(0 <= min(n, k));
return n >= k ? (n & k) == k : false;
}
// Number of ways to place n '('s and k ')'s starting with m copies of '(' such that in each prefix, number of '(' is equal or greater than ')'
// Catalan(n, n, 0): n-th catalan number
// Catalan(s, s+k-1, k-1): sum of products of k catalan numbers where the index of product sums up to s.
// O(1)
T Catalan(int n, int k, int m = 0) const{
ASSERT(0 <= min({n, k, m}));
return k <= m ? C(n + k, k) : k <= n + m ? C(n + k, k) - C(n + k, k - m - 1) : T();
}
#undef ASSERT
};
// T must be of modular type
// mod must be a prime
// Requires modular
template<class T>
struct number_theoric_transform_wrapper{
// i \in [2^k, 2^{k+1}) holds w_{2^k+1}^{i-2^k}
static vector<T> root, buffer1, buffer2;
static void adjust_root(int n){
if(root.empty()) root = {1, 1};
for(auto k = (int)root.size(); k < n; k <<= 1){
root.resize(n, 1);
T w = T::primitive_root().power((T::mod() - 1) / (k << 1));
for(auto i = k; i < k << 1; ++ i) root[i] = i & 1 ? root[i >> 1] * w : root[i >> 1];
}
}
// n must be a power of two
// p must have next n memories allocated
// O(n * log(n))
template<class T_ptr>
static void transform(int n, T_ptr p, bool invert = false){
assert(n && __builtin_popcount(n) == 1 && (T::mod() - 1) % n == 0);
for(auto i = 1, j = 0; i < n; ++ i){
int bit = n >> 1;
for(; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if(i < j) swap(p[i], p[j]);
}
adjust_root(n);
for(auto len = 1; len < n; len <<= 1) for(auto i = 0; i < n; i += len << 1) for(auto j = 0; j < len; ++ j){
T x = p[i + j], y = p[len + i + j] * root[len + j];
p[i + j] = x + y, p[len + i + j] = x - y;
}
if(invert){
reverse(p + 1, p + n);
T inv_n = T(1) / n;
for(auto i = 0; i < n; ++ i) p[i] *= inv_n;
}
}
// Double the length of the ntt array
// n must be a power of two
// p must have next 2n memories allocated
// O(n * log(n))
template<class T_ptr>
static void double_up(int n, T_ptr p){
assert(n && __builtin_popcount(n) == 1 && (T().mod() - 1) % (n << 1) == 0);
buffer1.resize(n << 1);
for(auto i = 0; i < n; ++ i) buffer1[i << 1] = p[i];
transform(n, p, true);
adjust_root(n << 1);
for(auto i = 0; i < n; ++ i) p[i] *= root[n | i];
transform(n, p);
for(auto i = 0; i < n; ++ i) buffer1[i << 1 | 1] = p[i];
copy(buffer1.begin(), buffer1.begin() + 2 * n, p);
}
static vector<T> res;
// O(n * m)
template<class T_ptr_l, class T_ptr_r>
static void _convolute_naive(int n, const T_ptr_l p, int m, const T_ptr_r q){
res.assign(max(0, n + m - 1), T{0});
for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res[i + j] += p[i] * q[j];
}
// O((n + m) * log(n + m))
template<class T_ptr_l, class T_ptr_r>
static void convolute(int n, const T_ptr_l p, int m, const T_ptr_r q){
if(min(n, m) < 55){
_convolute_naive(n, p, m, q);
return;
}
int len = n + m - 1, clen = 1 << __lg(len) + (__builtin_popcount(len) != 1);
buffer1.assign(clen, 0);
copy(p, p + n, buffer1.begin());
transform(buffer1);
buffer2.assign(clen, 0);
copy(q, q + m, buffer2.begin());
transform(buffer2);
for(auto i = 0; i < clen; ++ i) buffer1[i] *= buffer2[i];
transform(buffer1, true);
res.resize(len);
copy(buffer1.begin(), buffer1.begin() + len, res.begin());
}
// O(n * m)
template<class T_ptr_l, class T_ptr_r>
static void _cross_correlation_naive(int n, const T_ptr_l p, int m, const T_ptr_r q){
assert(n >= m);
res.assign(n - m + 1, T{0});
for(auto i = 0; i <= n - m; ++ i) for(auto j = 0; j < m; ++ j) res[i] += p[i + j] * q[j];
}
// O(m * log(m))
template<class T_ptr_l, class T_ptr_r>
static void cross_correlation(int n, const T_ptr_l p, int m, const T_ptr_r q){
assert(n >= m);
if(m < 55){
_cross_correlation_naive(n, p, m, q);
return;
}
int len = n, clen = 1 << __lg(len) + (__builtin_popcount(len) != 1);
buffer1.assign(clen, 0);
copy(p, p + n, buffer1.begin());
transform(buffer1);
buffer2.assign(clen, 0);
copy(q, q + m, buffer2.begin());
reverse(buffer2.begin(), buffer2.begin() + m);
transform(buffer2);
for(auto i = 0; i < clen; ++ i) buffer1[i] *= buffer2[i];
transform(buffer1, true);
res.resize(n - m + 1);
copy(buffer1.begin() + m - 1, buffer1.begin() + n, res.begin());
}
// O((n + m) * log(n + m))
template<class T_ptr>
static void square(int n, const T_ptr p){
if(n < 40){
convolute_naive(n, p, n, p);
return;
}
int len = 2 * n - 1, clen = 1 << __lg(len) + 1;
buffer1.assign(clen, 0);
copy(p, p + n, buffer1.begin());
transform(buffer1);
for(auto i = 0; i < clen; ++ i) buffer1[i] *= buffer1[i];
transform(buffer1, true);
res.resize(len);
copy(buffer1.begin(), buffer1.begin() + len, res.begin());
}
static void transform(vector<T> &p, bool invert = false){
transform((int)p.size(), p.data(), invert);
}
static void double_up(vector<T> &p){
int n = (int)p.size();
p.resize(n << 1);
double_up(n, p.data());
}
// O((n + m) * log(n + m))
static vector<T> convolute(const vector<T> &p, const vector<T> &q){
convolute((int)p.size(), p.data(), (int)q.size(), q.data());
return res;
}
// O(m * log(m))
static vector<T> cross_correlation(const vector<T> &p, const vector<T> &q){
assert((int)p.size() >= (int)q.size());
cross_correlation((int)p.size(), p.data(), (int)q.size(), q.data());
return res;
}
// O((n + m) * log(n + m))
static vector<T> square(const vector<T> &p){
square((int)p.size(), p.data(), (int)p.size(), p.data());
return res;
}
// O((n + m) * log(n + m))
static vector<T> arbitrarily_convolute(const vector<T> &p, const vector<T> &q){
using modular0 = modular_fixed_base<unsigned int, 880803841>;
using modular1 = modular_fixed_base<unsigned int, 897581057>;
using modular2 = modular_fixed_base<unsigned int, 998244353>;
using ntt0 = number_theoric_transform_wrapper<modular0>;
using ntt1 = number_theoric_transform_wrapper<modular1>;
using ntt2 = number_theoric_transform_wrapper<modular2>;
vector<modular0> p0((int)p.size()), q0((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p0[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q0[i] = q[i].data;
auto xy0 = ntt0::convolute(p0, q0);
vector<modular1> p1((int)p.size()), q1((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p1[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q1[i] = q[i].data;
auto xy1 = ntt1::convolute(p1, q1);
vector<modular2> p2((int)p.size()), q2((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p2[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q2[i] = q[i].data;
auto xy2 = ntt2::convolute(p2, q2);
static const modular1 r01 = 1 / modular1(modular0::mod());
static const modular2 r02 = 1 / modular2(modular0::mod());
static const modular2 r12 = 1 / modular2(modular1::mod());
static const modular2 r02r12 = r02 * r12;
static const T w1 = modular0::mod();
static const T w2 = w1 * modular1::mod();
int n = (int)p.size() + (int)q.size() - 1;
vector<T> res(n);
for(auto i = 0; i < n; ++ i){
using ull = unsigned long long;
ull a = xy0[i].data;
ull b = (xy1[i].data + modular1::mod() - a) * r01.data % modular1::mod();
ull c = ((xy2[i].data + modular2::mod() - a) * r02r12.data + (modular2::mod() - b) * r12.data) % modular2::mod();
res[i] = xy0[i].data + w1 * b + w2 * c;
}
return res;
}
};
template<class T> vector<T> number_theoric_transform_wrapper<T>::root;
template<class T> vector<T> number_theoric_transform_wrapper<T>::buffer1;
template<class T> vector<T> number_theoric_transform_wrapper<T>::buffer2;
template<class T> vector<T> number_theoric_transform_wrapper<T>::res;
using ntt = number_theoric_transform_wrapper<modular>;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int n, qn;
cin >> n >> qn;
vector<modular> a(n);
copy_n(istream_iterator<modular>(cin), n, a.begin());
vector<array<int, 2>> update;
for(auto qi = 0; qi < qn; ++ qi){
int type;
cin >> type;
if(type == 1){
int k, x;
cin >> k >> x;
auto power = modular::precalc_power(k, n);
vector<modular> coef(n);
modular c = 1;
for(auto i = 0; i < n; ++ i){
coef[i] = power[i] * c;
c = c * (x + i) / (i + 1);
}
a = ntt::convolute(a, coef);
a.resize(n);
}
else{
int i;
cin >> i, -- i;
cout << a[i] << "\n";
}
}
return 0;
}
/*
*/