結果
| 問題 |
No.2498 OX Operations
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-07 02:28:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,972 ms / 4,000 ms |
| コード長 | 10,808 bytes |
| コンパイル時間 | 4,304 ms |
| コンパイル使用メモリ | 265,696 KB |
| 最終ジャッジ日時 | 2025-02-17 06:00:32 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;
double EPS = 1e-15;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
inline int msb(__int128 n) { return (n >> 64) != 0 ? (127 - __builtin_clzll((ll)(n >> 64))) : n != 0 ? (63 - __builtin_clzll((ll)(n))) : -1; }
#define gcd __gcd
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif
//【bitアフィン 作用付き XOR モノイド】
/*
* S ∋ x = {v, ~0} : 奇数個の元の XOR で値 v をとっていることを表す.
* F ∋ f = {a, b} : 一次関数 f(x) = (a AND x) XOR b を表す.
* a OR x = (~a AND x) XOR a
* x op y : 値 vx + vy をとっている状態にする.
* f act x : 値 f(v) をとっている状態にする.
* f comp g : 合成した一次関数 f o g を返す.
*/
using T127 = int;
using S127 = pair<T127, T127>; // ベクトル (v, c)
using F127 = pair<T127, T127>; // 行列 (a, b; 0, 1)
S127 op127(S127 x, S127 y) {
auto [vx, cx] = x; // ベクトル (vx, cx)
auto [vy, cy] = y; // ベクトル (vy, cy)
// (vx, cx) + (vy, cy) = (vx + vy, cx + cy)
return { vx ^ vy, cx ^ cy };
}
S127 e127() { return { 0, 0 }; }
S127 act127(F127 f, S127 x) {
auto [v, c] = x; // ベクトル (v, c)
auto [a, b] = f; // 行列 (a, b; 0, 1)
// (a, b; 0, 1).(v, c) = (a v + b c, c)
return { (a & v) ^ (b & c), c };
}
F127 comp127(F127 f, F127 g) {
auto [a, b] = f; // 行列 (a, b; 0, 1)
auto [c, d] = g; // 行列 (c, d; 0, 1)
// (a, b; 0, 1).(c, d; 0, 1) = (a c, a d + b; 0, 1)
return { (a & c), (a & d) ^ b };
}
F127 id127() { return { ~0, 0 }; }
#define BitAffine_XOR_mmonoid S127, op127, e127, F127, act127, comp127, id127
//【上から状態桁 DP,未満フラグ,数え上げ】O(n b m)(の改変)
/*
* b=10 進数で n 桁の数 num 以下の非負の整数で,数字和が m の倍数であるものの個数を返す.
*/
vm count_digit_sum(const string& num, const string& a0, const string& a1, int b = 2) {
// 参考 : https://ferin-tech.hatenablog.com/entry/2019/11/10/%E6%A1%81DP%E3%81%AE%E5%AE%9F%E8%A3%85
// verify : https://atcoder.jp/contests/dp/tasks/dp_s
int n = sz(num);
// dump(n);
// dp[i][f][j] : 以下の条件を満たす数の個数:
// i : 上からの桁 d[0..i) まで決まっている.
// f : d[0..i) < num[0..i) なら 1,さもなくば 0(未満フラグ)
// j : d[0..i) の popcount
int K = 30;
vvvm dp(n + 1, vvm(1LL << 1, vm(K + 1)));
dp[0][0][0] = 1;
// 上の桁から順に配る DP
rep(i, n) {
// x : num の上から i 桁目の数
int x = num[i] - '0';
repb(f, 1) {
// d_max : d[i] のとれる値の最大値
int d_max = (f ? b - 1 : x);
repi(j, 0, K) {
// d : d[i]
repi(d, 0, d_max) {
int nf = (int)(f || (d < d_max));
int nj = j;
if (d == 0) nj += a0[i] - '0';
else nj += a1[i] - '0';
if (nj <= K) dp[i + 1][nf][nj] += dp[i][f][j];
}
}
}
// dump(i + 1);
// dump("!smaller"); dump(dp[i + 1][0]);
// dump("smaller"); dump(dp[i + 1][1]);
}
vm res(K + 1);
repi(j, 0, K) res[j] += dp[n][0][j] + dp[n][1][j];
return res;
}
//【ゼータ変換(下位要素)】O(n)
/*
* a[0..n) を
* A[i] = Σj≦i a[j]
* なる A[0..n) に上書きする(下位要素の値全てを自身に加える)
*/
template <typename T>
void leq_zeta(vector<T>& a) {
// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
//【例(n = 8 のとき)】:
// A[0] = a[0]
// A[1] = a[0] + a[1]
// A[2] = a[0] + a[1] + a[2]
// A[3] = a[0] + a[1] + a[2] + a[3]
// A[4] = a[0] + a[1] + a[2] + a[3] + a[4]
// A[5] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5]
// A[6] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]
// A[7] = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7]
int n = sz(a);
repi(i, 1, n - 1) a[i] += a[i - 1];
}
//【メビウス変換(下位要素)】O(n)
/*
* A[0..n) を
* A[i] = Σj≦i a[j]
* なる a[0..n) に上書きする(下位要素から自身の値への寄与を取り除く)
*/
template <typename T>
void leq_mobius(vector<T>& A) {
// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
//【例(n = 8 のとき)】:
// a[0] = + A[0]
// a[1] = - A[0] + A[1]
// a[2] = - A[1] + A[2]
// a[3] = - A[2] + A[3]
// a[4] = - A[3] + A[4]
// a[5] = - A[4] + A[5]
// a[6] = - A[5] + A[6]
// a[7] = - A[6] + A[7]
int n = sz(A);
repir(i, n - 1, 1) A[i] -= A[i - 1];
}
//【添字 max での畳込み】O(n)(の改変)
/*
* 与えられた a[0..n), b[0..n) に対して
* c[k] = Σ_(max(i,j) = k) a[i] b[j]
* なる c[0..n) を返す.
*
* 利用:【ゼータ変換(下位要素)】,【メビウス変換(下位要素)】
*/
template <typename T>
vector<T> max_convolution(vector<vector<T>>& a) {
// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
int n = sz(a), K = 30;
rep(i, n) leq_zeta(a[i]);
vm b(K + 1, 1);
rep(i, n) repi(j, 0, K) b[j] *= a[i][j];
leq_mobius(b);
return b;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int n, q;
cin >> n >> q;
vi m(n);
cin >> m;
vector<S127> ini0(n, { 0, ~0 }), ini1(n, { ~0, ~0 });
lazy_segtree<BitAffine_XOR_mmonoid> seg0(ini0), seg1(ini1);
dump(seg0); dump(seg1);
rep(hoge, q) {
char c; int l, r, x;
cin >> c >> l >> r >> x;
l--;
if (c == 'o') {
seg0.apply(l, r, { ~x, x });
seg1.apply(l, r, { ~x, x });
}
else {
seg0.apply(l, r, { ~0, x });
seg1.apply(l, r, { ~0, x });
}
dump(seg0); dump(seg1);
}
vi a0(n), a1(n);
rep(i, n) {
a0[i] = seg0.get(i).first;
a1[i] = seg1.get(i).first;
}
dump(a0); dump(a1);
int K = 30;
vvm cnt(n);
rep(i, n) cnt[i] = count_digit_sum(bitset<30>(m[i]).to_string(),
bitset<30>(a0[i]).to_string(), bitset<30>(a1[i]).to_string());
dumpel(cnt);
auto Cnt = max_convolution(cnt);
dump(Cnt);
mint res = 0;
repi(j, 0, K) res += Cnt[j] * j;
cout << res << endl;
}