1906K - Deck-Building Game - CodeForces Solution


divide and conquer math

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;

const int N = 1 << 17;
const int M = 1 << 18;
const int mod = 998244353;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int n, num[N];
VI g[M][2], t1, t2, t3, t4;
LL pw[M], f[M], ff[M], inv[M];
LL C(int n, int m) {
	LL res = (f[n] * inv[m]) % mod;
	res = (res * inv[n - m]) % mod;
	return res;
}
int pow_mod(int a, LL e) {
	int res = 1;
	for (; e; a = (LL)a * a % mod, e >>= 1) if (e & 1) res = (LL)res * a % mod;
	return res;
}
void fwt_xor(VI &a, int length, int type) {
	int len = -1;
	for (int x = length; x; ++len, x >>= 1);
	repn(i, 1, len) for (int j = 0; j < length; j += 1 << i)
		for (int k = j, szk = 1 << i - 1; k < j + szk; ++k) {
			int s = a[k], t = a[k + szk];
			a[k] = s + t >= mod ? s + t - mod : s + t;
			a[k + szk] = s - t < 0 ? s - t + mod : s - t;
		}
	if (type == 1) return;
	int inv = pow_mod(length, mod - 2);
	rep(i, 0, length) a[i] = 1LL * a[i] * inv % mod;
}
void work(int k, int l, int r) {
	if (l + 1 == r) {
		g[k][0].resize(1), g[k][1].resize(1);
		repn(i, 0, num[l]) {
			int p = i & 1;
			g[k][p][0] = (g[k][p][0] + C(num[l], i) * pw[i]) % mod;
		}
		return;
	}
	int len = (r - l) >> 1;
	work(LC, l, l + len);
	work(RC, l + len, r);
	len <<= 1;
	g[LC][0].resize(len), g[LC][1].resize(len);
	g[RC][0].resize(len), g[RC][1].resize(len);
	rep(i, 0, len / 2) g[RC][1][i + len / 2] = g[RC][1][i], g[RC][1][i] = 0;
	fwt_xor(g[LC][0], len, 1), fwt_xor(g[LC][1], len, 1);
	fwt_xor(g[RC][0], len, 1), fwt_xor(g[RC][1], len, 1);
	t1.clear(), t1.resize(len);
	t2.clear(), t2.resize(len);
	t3.clear(), t3.resize(len);
	t4.clear(), t4.resize(len);
	rep(i, 0, len) {
		t1[i] = (LL)g[LC][0][i] * g[RC][0][i] % mod;
		t2[i] = (LL)g[LC][0][i] * g[RC][1][i] % mod;
		t3[i] = (LL)g[LC][1][i] * g[RC][0][i] % mod;
		t4[i] = (LL)g[LC][1][i] * g[RC][1][i] % mod;
	}
	fwt_xor(t1, len, -1), fwt_xor(t2, len, -1), fwt_xor(t3, len, -1), fwt_xor(t4, len, -1);
	g[k][0].resize(len), g[k][1].resize(len);
	if (len == r) {
		rep(i, 0, len) g[k][0][i] = ((LL)t1[i] + t2[i] + t3[i] + t4[i]) % mod;
	}
	else {
		rep(i, 0, len) {
			g[k][0][i] = (t1[i] + t4[i]) % mod;
			g[k][1][i] = (t2[i] + t3[i]) % mod;
		}
	}
}
int main() {
	IO;
	pw[0] = 1;
	rep(i, 1, M) pw[i] = pw[i - 1] * 2 % mod;
	f[0] = 1;
	rep(i, 1, M) f[i] = (f[i - 1] * i) % mod;
	ff[1] = ff[0] = inv[1] = inv[0] = 1;  
	rep(i, 2, M) {
    	inv[i] = (LL)(mod - mod / i) * inv[mod % i] % mod;
    	ff[i] = inv[i];
	}
	rep(i, 2, M) inv[i] = (inv[i - 1] * inv[i]) % mod;
	cin >> n;
	repn(i, 1, n) {
		int x;
		cin >> x;
		num[x]++;
	}
	work(1, 0, N);
	cout << g[1][0][0] << "\n";
	return 0;
}


Comments

Submit
0 Comments
More Questions

1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array