結果
問題 |
No.2990 Interval XOR
|
ユーザー |
|
提出日時 | 2025-04-29 13:47:16 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 274 ms / 2,000 ms |
コード長 | 17,049 bytes |
コンパイル時間 | 3,977 ms |
コンパイル使用メモリ | 297,044 KB |
実行使用メモリ | 13,988 KB |
最終ジャッジ日時 | 2025-04-29 13:47:28 |
合計ジャッジ時間 | 11,855 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
// #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 debug_bin(...) 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 constexpr 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 constexpr 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 constexpr 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 constexpr 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(){ } constexpr modular_fixed_base(const double &x){ _data = _normalize(llround(x)); } constexpr modular_fixed_base(const long double &x){ _data = _normalize(llround(x)); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> constexpr modular_fixed_base(const T &x){ _data = _normalize(x); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static constexpr 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> constexpr operator T() const{ return data(); } constexpr modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((_data += otr._data) >= _mod) _data -= _mod; return *this; } constexpr 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> constexpr modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> constexpr modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); } constexpr modular_fixed_base &operator++(){ return *this += 1; } constexpr modular_fixed_base &operator--(){ return *this += _mod - 1; } constexpr modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; } constexpr modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; } constexpr modular_fixed_base operator-() const{ return modular_fixed_base(_mod - _data); } constexpr 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> constexpr 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> constexpr 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> constexpr 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> constexpr modular_fixed_base geometric_sum(T e, modular_fixed_base c = 1) const{ return modular_fixed_base(*this).inplace_geometric_sum(e, c); } // Returns the minimum integer e>0 with b^e=*this, if it exists // O(sqrt(mod)) applications of hashmap constexpr optional<data_t> log(const modular_fixed_base &b) const{ data_t m = mod(), n = sqrtl(m) + 1, j = 1; modular_fixed_base e = 1, f = 1; unordered_map<data_t, data_t> A; while(j <= n && (f = e *= b) != *this) A[(e * *this).data()] = j ++; if(e == *this) return j; if(gcd(mod(), e.data()) == gcd(mod(), data())) for(auto i = 2; i < n + 2; ++ i) if(A.count((e *= f).data())) return n * i - A[e.data()]; return {}; } constexpr optional<modular_fixed_base> inverse() const{ make_signed_t<data_t> a = data(), m = _mod, u = 0, v = 1; if(data() < _INV.size()) return _INV[data()]; while(a){ make_signed_t<data_t> t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } if(m != 1) return {}; return modular_fixed_base{u}; } modular_fixed_base &operator/=(const modular_fixed_base &otr){ auto inv_ptr = otr.inverse(); assert(inv_ptr); return *this = *this * *inv_ptr; } #define ARITHMETIC_OP(op, apply_op)\ constexpr 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>\ constexpr 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>\ constexpr 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)\ constexpr 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>\ constexpr 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>\ constexpr 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; constexpr data_t data() const{ return _data; } #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; constexpr unsigned int mod = (119 << 23) + 1; // 998244353 // constexpr unsigned int mod = 1e9 + 7; // 1000000007 // constexpr unsigned int mod = 1e9 + 9; // 1000000009 // constexpr unsigned long long mod = (unsigned long long)1e18 + 9; using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>; constexpr modular operator""_m(const char *x){ modular res = 0; long long buffer = 0; long long buffer_width = 1; constexpr long long buffer_th = 1'000'000'000'000'000'000LL; while(*x){ #ifdef LOCAL assert(isdigit(*x)); #endif buffer = buffer * 10 + (*(x ++) - '0'); if((buffer_width *= 10) == buffer_th){ res = buffer_width * res + buffer; buffer = 0; buffer_width = 1; } } res = buffer_width * res + buffer; return res; } template<class T, class _transform_1D> struct fast_fourier_transform_multidimensional_wrapper_base{ static vector<T> buffer1, buffer2; // Assumes that n is a power of _transform_1D::length // O(n * log(n)) static void transform(int n, T *p, bool invert = false){ #ifdef LOCAL int power = 1; while(power < n) power *= _transform_1D::length; assert(power == n); #endif if(!invert){ for(auto len = 1; len < n; len *= _transform_1D::length) for(auto i = 0; i < n; i += _transform_1D::length * len) for(auto j = 0; j < len; ++ j) _transform_1D::transform(p + i + j, len); } else{ for(auto len = 1; len < n; len *= _transform_1D::length) for(auto i = 0; i < n; i += _transform_1D::length * len) for(auto j = 0; j < len; ++ j) _transform_1D::inverse_transform(p + i + j, len); T inv = 1 / _transform_1D::coefficient(n); if(inv != T{1}) for(auto i = 0; i < n; ++ i) p[i] *= inv; } } // O(n * log(n)) static void transform(vector<T> &p, bool invert = false){ transform((int)p.size(), p.data(), invert); } // O(n * log(n)) static vector<T> convolve(const vector<T> &p, const vector<T> &q){ int n = (int)p.size(); assert((int)q.size() == n); buffer1.resize(n, 0); copy(p.begin(), p.end(), buffer1.begin()); transform(buffer1); buffer2.resize(n, 0); copy(q.begin(), q.end(), buffer2.begin()); transform(buffer2); for(auto i = 0; i < n; ++ i) buffer1[i] *= buffer2[i]; transform(buffer1, true); return vector<T>(buffer1.begin(), buffer1.begin() + n); } // Destroy p and q // Store the result on p // O(n * log(n)) static void inplace_convolve(int n, T *p, T *q){ transform(n, p), transform(n, q); for(auto i = 0; i < n; ++ i) p[i] *= q[i]; transform(n, p, true); } // Destroy p and q // Store the result on p // O(n * log(n)) static void inplace_square(int n, T *p){ transform(n, p); for(auto i = 0; i < n; ++ i) p[i] *= p[i]; transform(n, p, true); } }; template<class T, class U> vector<T> fast_fourier_transform_multidimensional_wrapper_base<T, U>::buffer1; template<class T, class U> vector<T> fast_fourier_transform_multidimensional_wrapper_base<T, U>::buffer2; template<class T> struct _transform_1D_bitwise_xor{ static constexpr int length = 2; static void transform(T *a, int len){ tie(a[0], a[len]) = tuple{ a[0] + a[len], a[0] - a[len], }; } static void inverse_transform(T *a, int len){ tie(a[0], a[len]) = tuple{ a[0] + a[len], a[0] - a[len], }; } // inverse_transform(transform(p)) = coefficient(len(p)) * p static T coefficient(int n){ return n; } }; template<class T> struct _transform_1D_bitwise_and{ static constexpr int length = 2; static void transform(T *a, int len){ a[0] += a[len]; } static void inverse_transform(T *a, int len){ a[0] -= a[len]; } // inverse_transform(transform(p)) = coefficient(len(p)) * p static T coefficient(int n){ return 1; } }; template<class T> struct _transform_1D_bitwise_or{ static constexpr int length = 2; static void transform(T *a, int len){ a[len] += a[0]; } static void inverse_transform(T *a, int len){ a[len] -= a[0]; } // inverse_transform(transform(p)) = coefficient(len(p)) * p static T coefficient(int n){ return 1; } }; template<class T> struct _transform_1D_tritwise_addition_modular{ static_assert(T::mod() % 3 == 1); static constexpr int length = 3; static const T root; static const T root_sq; static void transform(T *a, int len){ tie(a[0], a[len], a[len << 1]) = tuple{ a[0] + a[len] + a[len << 1], a[0] + root * a[len] + root_sq * a[len << 1], a[0] + root_sq * a[len] + root * a[len << 1] }; } static void inverse_transform(T *a, int len){ tie(a[0], a[len], a[len << 1]) = tuple{ a[0] + a[len] + a[len << 1], a[0] + root_sq * a[len] + root * a[len << 1], a[0] + root * a[len] + root_sq * a[len << 1] }; } // inverse_transform(transform(p)) = coefficient(len(p)) * p static T coefficient(int n){ return n; } }; template<class T> const T _transform_1D_tritwise_addition_modular<T>::root = T::primitive_root().power((T::mod() - 1) / 3); template<class T> const T _transform_1D_tritwise_addition_modular<T>::root_sq = root * root; template<class T> using bitwise_xor_transform = fast_fourier_transform_multidimensional_wrapper_base<T, _transform_1D_bitwise_xor<T>>; template<class T> using bitwise_and_transform = fast_fourier_transform_multidimensional_wrapper_base<T, _transform_1D_bitwise_and<T>>; template<class T> using bitwise_or_transform = fast_fourier_transform_multidimensional_wrapper_base<T, _transform_1D_bitwise_or<T>>; template<class T> using tritwise_addition_transform = fast_fourier_transform_multidimensional_wrapper_base<T, _transform_1D_tritwise_addition_modular<T>>; template<class T> using fwht = bitwise_xor_transform<T>; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); auto power = modular::precalc_power(2, 1 << 18); int n, m; cin >> m >> n; vector<array<int, 2>> inter(n); vector<modular> f(1 << m); f[0] = 1; for(auto &[l, r]: inter){ cin >> l >> r, ++ r; f[0] *= r - l; } vector<array<modular, 2>> coef; for(auto lsb = 0; lsb < m; ++ lsb){ int base_exponent = 0; coef.assign(1 << m - lsb - 1, array{1_m, 0_m}); for(auto [l, r]: inter){ int l_start = l >> lsb + 1 << lsb + 1; int r_start = r >> lsb + 1 << lsb + 1; int l_mid = l >> lsb << lsb; int r_mid = r >> lsb << lsb; modular l_coef = (l_start == l_mid ? 1 : -1) * (2 * l_mid - l_start - l); modular r_coef = (r_start == r_mid ? -1 : 1) * (2 * r_mid - r_start - r); l >>= lsb + 1, r >>= lsb + 1; r %= 1 << m - lsb - 1; base_exponent ^= l; r ^= l; coef[r] = {coef[r][0] * l_coef + coef[r][1] * r_coef, coef[r][0] * r_coef + coef[r][1] * l_coef}; } for(auto i = 0; i < 1 << m - lsb - 1; ++ i){ coef[i] = { coef[i][0] + coef[i][1], coef[i][0] - coef[i][1], }; } for(auto len = 1; len < 1 << m - lsb - 1; len <<= 1){ for(auto i = 0; i < 1 << m - lsb - 1; i += len << 1){ for(auto j = i; j < i + len; ++ j){ tie(coef[j], coef[j + len]) = tuple{ array{coef[j][0] * coef[j + len][0], coef[j][1] * coef[j + len][1]}, array{coef[j][0] * coef[j + len][1], coef[j][1] * coef[j + len][0]} }; } } } for(auto k = 1 << lsb; k < 1 << m; k += 1 << lsb + 1){ f[k] = coef[k >> lsb + 1][0] * (__builtin_popcount(k >> lsb + 1 & base_exponent) & 1 ? -1 : 1); } } fwht<modular>::transform(f, true); ranges::copy(f, ostream_iterator<modular>(cout, "\n")); return 0; } /* (1+X+X^2)(X+X^2+X^3) sign(a,b) = (-1)^popcount(a & b) [X^k]T(f) = \sum_{i=0~2^m-1} sign(k,i) * f_i p[i] = X^0 + ... + X^{i-1} f[i] = p[r[i]] - p[l[i]] T(f[i]) = T(p[r[i]]) - T(p[l[i]]) Let b = lsb(k) [X^k]T(p[r]) = sign(k/2^{b+1},r/2^{b+1}) * \sum_{i=r/2^{b+1}*2^{b+1} ~ r-1} sign(2^b,i) = [X^{k/2^{b+1}}] T((\sum_{i=r/2^{b+1}*2^{b+1} ~ r-1} sign(2^b,i)) * X^{r/2^{b+1}}) */