Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m;
vector<long long> bs;
int cnt[(1 << 16) + 1];
long long inv2 = (998244353 + 1) / 2;
int popcount(long long x) { return __builtin_popcountll(x); }
bool add(long long x) {
for (auto& t : bs) x = min(x, t ^ x);
if (!x) return false;
for (int i = 0; i < ((int)(bs).size() + 1); ++i)
if (i == (int)(bs).size() || x > bs[i]) {
bs.insert(bs.begin() + i, x);
return true;
}
}
void build() {
for (int i = 0; i < (int)(bs).size(); i++) {
for (int j = i + 1; j < (int)(bs).size(); j++)
bs[i] = min(bs[i], bs[i] ^ bs[j]);
}
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
if (!(bs[i] >> l & 1)) {
int r = -1;
for (int j = l - 1; j >= 0; j--) {
if (bs[i] >> j & 1) {
r = j;
break;
}
}
for (int j = 0; j < (int)(bs).size(); j++) {
long long a = (bs[j] >> l & 1), b = (bs[j] >> r & 1);
if (a != b) bs[j] ^= (1LL << l) | (1LL << r);
}
}
}
while ((int)(bs).size() < m)
bs.push_back((1LL << (m - 1 - (int)(bs).size())));
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
for (int j = l + 1; j < (int)(bs).size(); j++) {
bs[i] |= (bs[m - 1 - j] >> (m - 1 - i) & 1) << j;
}
}
}
long long cur = 0;
long long pk = 1;
long long res[55], fres[55];
long long C[55][55], f[55][55];
void dfs(int d) {
if (d == (int)(bs).size()) {
res[popcount(cur)]++;
return;
}
dfs(d + 1);
cur ^= bs[d];
dfs(d + 1);
cur ^= bs[d];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 1; i < (1 << 16); i++) {
cnt[i] = cnt[i / 2] + i % 2;
}
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
if (!add(x)) pk *= 2, pk %= mod;
}
if ((int)(bs).size() <= 26) {
dfs(0);
for (int i = 0; i <= m; i++) cout << (pk * res[i] % mod + mod) % mod << " ";
cout << "\n";
} else {
for (int i = 0; i < 55; i++) C[i][i] = C[i][0] = 1;
for (int i = 0; i < 55; i++) {
for (int j = 1; j < i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
for (int b = 0; b <= m; b++) {
for (int c = 0; c <= m; c++) {
for (int a = 0; a <= b && a <= c; a++) {
int s = (a % 2 == 0 ? 1 : -1);
f[b][c] = (f[b][c] + s * C[b][a] * C[m - b][c - a]) % mod;
}
}
}
int k = (int)(bs).size();
build();
dfs(k);
long long pmk = 1;
for (int i = 0; i < m - k; i++) pk = (pk * inv2) % mod;
for (int c = 0; c <= m; c++) {
for (int b = 0; b <= m; b++) {
fres[c] += (f[b][c] * res[b]);
fres[c] %= mod;
}
}
for (int i = 0; i <= m; i++)
cout << (pk * fres[i] % mod + mod) % mod << " ";
cout << "\n";
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void read(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void readc(char &x) {
char c;
while (((c = getchar()) == ' ') || c == '\n')
;
x = c;
}
const int mod = 998244353;
int n, m, k;
long long f[70], g[70];
int s[70], ans[70];
int c[70][70], w[70][70];
vector<long long> t;
bool ins(long long x) {
int i;
for (((i)) = (((int)(m)) - 1); ((i)) >= (0); ((i))--) {
if ((x >> i) & 1) {
if (f[i])
x ^= f[i];
else
return f[i] = x, 1;
}
}
return 0;
}
void rebuild() {
int i, j;
for (((i)) = (0); ((i)) <= ((m)-1); ((i))++) {
for (((j)) = (0); ((j)) <= ((i)-1); ((j))++) {
if ((f[i] >> j) & 1) {
f[i] ^= f[j];
}
}
}
}
void dfs(int x, long long cur) {
if (x == t.size()) {
s[__builtin_popcountll(cur)]++;
return;
}
dfs(x + 1, cur);
dfs(x + 1, cur ^ t[x]);
}
int main() {
int i, j, p, pw = 1;
read(n);
read(m);
c[0][0] = 1;
for (((i)) = (1); ((i)) <= ((m)); ((i))++) {
c[i][0] = 1;
for (((j)) = (1); ((j)) <= ((i)); ((j))++) {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
long long x;
read(x);
if (ins(x))
k++;
else
pw = 2ll * pw % mod;
}
rebuild();
if (k <= 26) {
for (((i)) = (0); ((i)) <= ((m)-1); ((i))++) {
if (f[i]) t.push_back(f[i]);
}
dfs(0, 0);
for (((i)) = (0); ((i)) <= ((m + 1) - 1); ((i))++) {
printf("%d ", 1ll * s[i] * pw % mod);
}
} else {
for (((i)) = (0); ((i)) <= ((m)-1); ((i))++)
for (((j)) = (0); ((j)) <= ((m)-1); ((j))++) {
g[j] |= ((f[i] >> j) & 1) << i;
}
for (((i)) = (0); ((i)) <= ((m)-1); ((i))++) {
if (g[i] ^= (1ll << i)) {
t.push_back(g[i]);
}
}
dfs(0, 0);
for (((i)) = (0); ((i)) <= ((m + 1) - 1); ((i))++) {
for (((j)) = (0); ((j)) <= ((m + 1) - 1); ((j))++) {
for (((p)) = (0); ((p)) <= ((i + 1) - 1); ((p))++) {
if (p & 1) {
w[i][j] = (w[i][j] - 1ll * c[j][p] * c[m - j][i - p]) % mod;
if (w[i][j] < 0) w[i][j] += mod;
} else {
w[i][j] = (w[i][j] + 1ll * c[j][p] * c[m - j][i - p]) % mod;
}
}
ans[i] = (ans[i] + 1ll * w[i][j] * s[j]) % mod;
}
}
int t = n - m;
pw = 1;
if (t > 0) {
while (t--) pw = 2ll * pw % mod;
} else {
while (t++) pw = 1ll * (mod + 1) / 2 * pw % mod;
}
for (((i)) = (0); ((i)) <= ((m + 1) - 1); ((i))++) {
printf("%d ", 1ll * ans[i] * pw % mod);
}
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> answers(M + 1, 0);
int64_t value = 0;
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
return answers;
}
vector<int64_t> dp_on_repeats(vector<int64_t> basis, int M) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
}
sort(options.begin(), options.end());
vector<vector<mod_int>> dp(count_sum + 1, vector<mod_int>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
vector<int64_t> answers(M + 1, 0);
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
answers[count + __builtin_popcount(mask)] += (int64_t)dp[count][mask];
return answers;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(basis_obj.basis, basis_obj.basis + B);
vector<int64_t> answers =
B <= 0.59 * M ? generate_all(basis, M) : dp_on_repeats(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 200005, maxM = 60, p = 998244353;
void MOD(int &x) {
if (x >= p) x -= p;
}
int N, M, K;
long long A[maxM], B[maxM];
int pow2[maxN], ipow2[maxM];
void Insert(long long x) {
for (int i = M - 1; ~i; i--)
if ((x >> i) & 1) {
if (A[i])
x ^= A[i];
else {
A[i] = x, K++;
return;
}
}
}
long long G[maxM];
int len;
long long ans[maxM];
void DFS(int k, long long x) {
if (k == len) {
ans[__builtin_popcountll(x)]++;
return;
}
DFS(k + 1, x);
DFS(k + 1, x ^ G[k]);
}
int C[maxM][maxM], W[maxM][maxM];
int main() {
scanf("%d%d", &N, &M);
pow2[0] = ipow2[0] = 1;
for (int i = 1; i <= N; i++) {
long long tmp;
scanf("%lld", &tmp);
Insert(tmp);
pow2[i] = pow2[i - 1] << 1;
MOD(pow2[i]);
}
for (int i = 1; i <= M; i++)
ipow2[i] = 1LL * ipow2[i - 1] * ((p + 1) / 2) % p;
for (int i = 0; i < M; i++)
for (int j = 0; j < i; j++)
if (A[i] >> j & 1) A[i] ^= A[j];
if (K <= M / 2) {
for (int i = 0; i < M; i++)
if (A[i]) G[len++] = A[i];
DFS(0, 0);
for (int i = 0; i <= M; i++) ans[i] %= p;
for (int i = 0; i <= M; i++) printf("%d ", 1LL * ans[i] * pow2[N - K] % p);
return 0;
}
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) B[j] |= (A[i] >> j & 1) << i;
for (int i = 0; i < M; i++) {
B[i] ^= 1LL << i;
if (B[i]) G[len++] = B[i];
}
DFS(0, 0);
for (int i = 0; i <= M; i++) ans[i] %= p;
for (int i = 0; i <= M; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = C[i - 1][j] + C[i - 1][j - 1], MOD(C[i][j]);
}
for (int i = 0; i <= M; i++)
for (int j = 0; j <= M; j++)
for (int k = 0; k <= i; k++) {
if (k & 1)
W[i][j] += p - 1LL * C[j][k] * C[M - j][i - k] % p;
else
W[i][j] += 1LL * C[j][k] * C[M - j][i - k] % p;
MOD(W[i][j]);
}
int L = 1LL * pow2[N] * ipow2[M] % p;
for (int i = 0; i <= M; i++) {
int tmp = 0;
for (int j = 0; j <= M; j++) tmp += 1LL * ans[j] * W[i][j] % p, MOD(tmp);
printf("%d ", 1LL * tmp * L % p);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 100;
const long long inf = 0x3f3f3f3f;
const long long iinf = 1 << 30;
const long long linf = 2e18;
const long long mod = 998244353;
const double eps = 1e-7;
template <class T = long long>
T chmin(T &a, T b) {
return a = min(a, b);
}
template <class T = long long>
T chmax(T &a, T b) {
return a = max(a, b);
}
template <class T = long long>
inline void red(T &x) {
x -= mod, x += x >> 31 & mod;
}
template <class T = long long>
T read() {
T f = 1, a = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
a = (a << 3) + (a << 1) + ch - '0';
ch = getchar();
}
return a * f;
}
long long n, k, m, tot;
long long a[maxn], f[114], g[114], t[114];
long long ans[114], sum[114], fac[maxn], c[114][114], w[114][114];
void insert(long long x) {
for (long long i = (m); i >= (0); --i)
if (x >> i & 1) {
if (f[i])
x ^= f[i];
else
return (void)(f[i] = x, ++k);
}
}
long long cnt(long long x) { return __builtin_popcountll(x); }
void dfs(long long p, long long x) {
if (p == tot)
sum[cnt(x)]++;
else
dfs(p + 1, x), dfs(p + 1, x ^ t[p]);
}
signed main() {
n = read(), m = read(), fac[0] = 1, m--;
for (long long i = (1); i <= (n); ++i) red(fac[i] = fac[i - 1] * 2);
for (long long i = (1); i <= (n); ++i) a[i] = read(), insert(a[i]);
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (i - 1); ++j)
if (f[i] >> j & 1) f[i] ^= f[j];
if (k <= 26) {
for (long long i = (0); i <= (m); ++i)
if (f[i]) t[tot++] = f[i];
dfs(0, 0);
for (long long i = (0); i <= (m + 1); ++i)
printf("%lld\n", fac[n - k] * sum[i] % mod);
} else {
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (m); ++j) g[j] |= (f[i] >> j & 1) << i;
for (long long i = (0); i <= (m); ++i)
if (g[i] ^= 1ll << i) t[tot++] = g[i];
k = ++m - k, dfs(0, 0);
for (long long i = (0); i <= (m); ++i) c[i][0] = 1;
for (long long i = (1); i <= (m); ++i)
for (long long j = (1); j <= (m); ++j)
red(c[i][j] = c[i - 1][j] + c[i - 1][j - 1]);
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (m); ++j)
for (long long p = (0); p <= (i); ++p)
if (p & 1)
red(w[i][j] += mod - c[j][p] * c[m - j][i - p] % mod);
else
red(w[i][j] += c[j][p] * c[m - j][i - p] % mod);
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (m); ++j)
red(ans[i] += sum[j] * w[i][j] % mod);
long long coef = fac[max(0ll, n - m + k)];
for (long long i = (1); i <= (k); ++i) (coef *= ((mod + 1) / 2)) %= mod;
for (long long i = (0); i <= (m); ++i)
printf("%lld\n", ans[i] * coef % mod);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
const int mod = 998244353;
int mul(int a, int b) { return (long long)a * b % mod; }
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
const int maxm = 60;
int c[maxm][maxm];
void precalc() {
for (int i = 0; i < maxm; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j - 1];
add(c[i][j], c[i - 1][j]);
}
}
}
const int maxn = (int)2e5 + 5;
int n, m;
long long a[maxn];
bool read() {
if (scanf("%d%d", &n, &m) < 2) {
return false;
}
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
return true;
}
int k;
int bit[maxm];
long long x[maxm];
int l;
long long y[maxm];
int ans[maxm];
int ans0[maxm];
bool addVec(long long a) {
for (int i = 0; i < k; i++) {
if (a & (1ll << bit[i])) {
a ^= x[i];
}
}
if (!a) {
return false;
}
auto &b = bit[k];
b = 0;
while (!(a & (1ll << b))) {
b++;
}
for (int i = 0; i < k; i++) {
if (x[i] & (1ll << b)) {
x[i] ^= a;
}
}
x[k] = a;
k++;
return true;
}
void rec(int i, long long cur) {
if (i >= k) {
ans[__builtin_popcountll(cur)]++;
return;
}
rec(i + 1, cur);
rec(i + 1, cur ^ x[i]);
}
void solve() {
for (int i = 0; i <= m; i++) {
ans[i] = 0;
}
k = 0;
int tomul = 1;
for (int i = 0; i < n; i++) {
if (!addVec(a[i])) {
tomul = mul(tomul, 2);
}
}
if (k <= m / 2) {
rec(0, 0ll);
} else {
l = 0;
for (int i = 0; i < m; i++) {
{
bool found = false;
for (int j = 0; j < k; j++) {
if (bit[j] == i) {
found = true;
break;
}
}
if (found) {
continue;
}
}
auto &cur = y[l++];
cur = (1ll << i);
for (int j = 0; j < k; j++) {
if (x[j] & (1ll << i)) {
cur |= (1ll << bit[j]);
}
}
}
swap(x, y);
swap(k, l);
rec(0, 0ll);
for (int i = 0; i <= m; i++) {
ans0[i] = ans[i];
ans[i] = 0;
}
int inv2 = (mod + 1) / 2;
int todiv = 1;
for (int i = 0; i < k; i++) {
todiv = mul(todiv, inv2);
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
int cur = 0;
for (int cnt = 0; cnt <= i && cnt <= j; cnt++) {
int x = mul(c[j][cnt], c[m - j][i - cnt]);
if (cnt & 1) {
x = mul(x, mod - 1);
}
add(cur, x);
}
cur = mul(cur, ans0[j]);
add(ans[i], cur);
}
ans[i] = mul(ans[i], todiv);
}
}
for (int i = 0; i <= m; i++) {
ans[i] = mul(ans[i], tomul);
printf("%d ", ans[i]);
}
printf("\n");
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template <typename T>
void read(T& x) {
x = 0;
char ch = getchar();
long long f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
template <typename T, typename... Args>
void read(T& first, Args&... args) {
read(first);
read(args...);
}
int mod = 998244353, inv2 = (mod + 1) / 2;
inline int mul(int x, int y) { return 1ll * x * y % mod; }
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int sq(int x) { return 1ll * x * x % mod; }
int mpow(int a, int b) {
return b == 0 ? 1 : (b & 1 ? mul(a, sq(mpow(a, b / 2))) : sq(mpow(a, b / 2)));
}
int n, m;
long long a[200020], b[60], v[60], vn = 0;
int cnt[60], C[61][61];
void dfs(long long cv, int len) {
if (len == vn) {
++cnt[__builtin_popcountll(cv)];
return;
}
dfs(cv, len + 1);
dfs(cv ^ v[len], len + 1);
}
int main() {
read(n, m);
C[0][0] = 1;
for (int i = 1; i <= 60; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
}
for (int i = 0; i < n; i++) {
read(a[i]);
long long cc = a[i];
for (int j = m - 1; j >= 0; j--) {
if (b[j])
cc = min(cc, cc ^ b[j]);
else if ((cc >> j) & 1) {
b[j] = v[vn++] = cc;
break;
}
}
}
for (int j = 0; j < m; j++) {
for (int k = j + 1; k < m; k++)
if ((b[k] >> j) & 1) b[k] ^= b[j];
}
if (vn < m / 2) {
dfs(0, 0);
for (int i = 0; i <= m; i++)
cout << mul(cnt[i], mpow(2, n - vn)) << " \n"[i == m];
return 0;
}
vn = 0;
for (int i = 0; i < m; i++) {
if (!b[i]) {
long long cv = 1ll << i;
for (int j = i + 1; j < m; j++)
if ((b[j] >> i) & 1) cv |= 1ll << j;
v[vn++] = cv;
}
}
dfs(0, 0);
int ans[61] = {0};
for (int i = 0; i <= m; i++) {
int cv[61] = {0};
for (int j = 0; j <= i; j++) {
for (int k = 0; k <= m - i; k++) {
if (j & 1)
cv[j + k] = sub(cv[j + k], mul(C[i][j], C[m - i][k]));
else
cv[j + k] = add(cv[j + k], mul(C[i][j], C[m - i][k]));
}
}
for (int j = 0; j <= m; j++) {
ans[j] = add(ans[j], mul(mul(cv[j], cnt[i]),
mul(mpow(2, n - m + vn), mpow(inv2, vn))));
}
}
for (int j = 0; j <= m; j++) {
cout << ans[j] << " \n"[j == m];
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
void row_reduce(vector<int64_t> &basis) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
}
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> counts(M + 1, 0);
int64_t value = 0;
for (int mask = 0; mask < 1 << B; mask++) {
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
return counts;
}
vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int> highest_bit(B, -1);
vector<bool> bit_covered(M, false);
for (int i = 0; i < B; i++) {
highest_bit[i] = 63 - __builtin_clzll(basis[i]);
bit_covered[highest_bit[i]] = true;
}
vector<int64_t> orthogonal;
for (int bit = M - 1; bit >= 0; bit--)
if (!bit_covered[bit]) {
int64_t value = 1LL << bit;
for (int i = 0; i < B; i++)
value |= (basis[i] >> bit & 1) << highest_bit[i];
orthogonal.push_back(value);
}
return orthogonal;
}
vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> orthogonal = build_orthogonal(basis, M);
vector<int64_t> orthogonal_counts = generate_all(orthogonal, M);
vector<int64_t> counts(M + 1, 0);
vector<vector<int64_t>> choose(M + 1);
for (int n = 0; n <= M; n++) {
choose[n].resize(n + 1);
choose[n][0] = choose[n][n] = 1;
for (int r = 1; r < n; r++)
choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
}
auto &&get_choose = [&](int n, int r) -> int64_t {
if (r < 0 || r > n) return 0;
return choose[n][r];
};
for (int c = 0; c <= M; c++) {
vector<int64_t> w(M + 1, 0);
for (int d = 0; d <= M; d++)
for (int j = 0; j <= d; j++)
w[d] +=
(j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j);
for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d];
assert((counts[c] & ((1LL << (M - B)) - 1)) == 0);
counts[c] >>= M - B;
}
return counts;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(basis_obj.basis, basis_obj.basis + B);
vector<int64_t> answers =
B <= M / 2 ? generate_all(basis, M) : generate_from_orthogonal(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m;
vector<long long> bs;
int cnt[(1 << 16) + 1];
long long inv2 = (998244353 + 1) / 2;
int popcount(long long x) {
return cnt[x & 65535] + cnt[x >> 16 & 65535] + cnt[x >> 32 & 65535] +
cnt[x >> 48 & 65535];
}
bool add(long long x) {
for (auto& t : bs) x = min(x, t ^ x);
if (!x) return false;
for (int i = 0; i < ((int)(bs).size() + 1); ++i)
if (i == (int)(bs).size() || x > bs[i]) {
bs.insert(bs.begin() + i, x);
return true;
}
}
void build() {
for (int i = 0; i < (int)(bs).size(); i++) {
for (int j = i + 1; j < (int)(bs).size(); j++)
bs[i] = min(bs[i], bs[i] ^ bs[j]);
}
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
if (!(bs[i] >> l & 1)) {
int r = -1;
for (int j = l - 1; j >= 0; j--) {
if (bs[i] >> j & 1) {
r = j;
break;
}
}
for (int j = 0; j < (int)(bs).size(); j++) {
long long a = (bs[j] >> l & 1), b = (bs[j] >> r & 1);
if (a != b) bs[j] ^= (1LL << l) | (1LL << r);
}
}
}
while ((int)(bs).size() < m)
bs.push_back((1LL << (m - 1 - (int)(bs).size())));
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
for (int j = l + 1; j < (int)(bs).size(); j++) {
bs[i] |= (bs[m - 1 - j] >> (m - 1 - i) & 1) << j;
}
}
}
long long cur = 0;
long long pk = 1;
long long res[55], fres[55];
long long C[55][55], f[55][55];
void dfs(int d) {
if (d == (int)(bs).size()) {
res[popcount(cur)]++;
return;
}
dfs(d + 1);
cur ^= bs[d];
dfs(d + 1);
cur ^= bs[d];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 1; i < (1 << 16); i++) {
cnt[i] = cnt[i / 2] + i % 2;
}
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
if (!add(x)) pk *= 2, pk %= mod;
}
if ((int)(bs).size() <= 26) {
dfs(0);
for (int i = 0; i <= m; i++) cout << (pk * res[i] % mod + mod) % mod << " ";
cout << "\n";
} else {
for (int i = 0; i < 55; i++) C[i][i] = C[i][0] = 1;
for (int i = 0; i < 55; i++) {
for (int j = 1; j < i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
for (int b = 0; b <= m; b++) {
for (int c = 0; c <= m; c++) {
for (int a = 0; a <= b && a <= c; a++) {
int s = (a % 2 == 0 ? 1 : -1);
f[b][c] = (f[b][c] + s * C[b][a] * C[m - b][c - a]) % mod;
}
}
}
int k = (int)(bs).size();
build();
dfs(k);
long long pmk = 1;
for (int i = 0; i < m - k; i++) pk = (pk * inv2) % mod;
for (int c = 0; c <= m; c++) {
for (int b = 0; b <= m; b++) {
fres[c] += (f[b][c] * res[b]);
fres[c] %= mod;
}
}
for (int i = 0; i <= m; i++)
cout << (pk * fres[i] % mod + mod) % mod << " ";
cout << "\n";
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmin(T &x, T y) {
return x > y ? x = y, 1 : 0;
}
template <typename T>
bool chkmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
long long readint() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int cys = 998244353;
int n, m, k;
long long a[60], f[60], C[60][60], d[60][60], p[60];
long long mod(long long x) { return x >= cys ? x - cys : x; }
void dfs(int u, int t, long long x) {
if (u == t)
p[__builtin_popcountll(x)]++;
else
dfs(u + 1, t, x), dfs(u + 1, t, x ^ f[u]);
}
void insert(long long x) {
for (int i = m - 1; i >= 0; i--) {
if (x & (1ll << i)) {
if (a[i])
x ^= a[i];
else {
a[i] = x;
return;
}
}
}
}
int main() {
n = readint();
m = readint();
for (int i = 1; i <= n; i++) insert(readint());
for (int i = 0; i < m; i++)
if (a[i])
for (int j = i + 1; j < m; j++)
if ((a[j] >> i) & 1) a[j] ^= a[i];
for (int i = 0; i < m; i++) {
if (a[i]) {
if (i != k) {
for (int j = 0; j < m; j++) {
if (!a[j]) continue;
int ti = (a[j] >> i) & 1, tk = (a[j] >> k) & 1;
if (ti) a[j] ^= 1ll << i;
if (tk) a[j] ^= 1ll << k;
if (ti) a[j] ^= 1ll << k;
if (tk) a[j] ^= 1ll << i;
}
}
k++;
}
}
k = 0;
for (int i = 0; i < m; i++)
if (a[i]) f[k++] = a[i];
for (int i = k; i < m; i++) f[i] = 1ll << i;
for (int i = k; i < m; i++)
for (int j = 0; j < k; j++)
if ((f[j] >> i) & 1) f[i] ^= 1ll << j;
if (k <= 27) {
dfs(0, k, 0);
long long ans = 1;
for (int i = 0; i < n - k; i++) ans = ans * 2 % cys;
for (int i = 0; i <= m; i++) printf("%lld ", ans * p[i] % cys);
printf("\n");
} else {
dfs(k, m, 0);
for (int i = 0; i <= m; i++) C[i][0] = 1;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= i; j++) C[i][j] = mod(C[i - 1][j] + C[i - 1][j - 1]);
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
for (int l = 0; l <= i; l++) {
long long tmp = C[j][l] * C[m - j][i - l] % cys;
if (l & 1)
d[i][j] = mod(d[i][j] + cys - tmp);
else
d[i][j] = mod(d[i][j] + tmp);
}
}
}
for (int i = 0; i <= m; i++) {
long long ans = 0;
for (int j = 0; j <= m; j++) ans = (ans + p[j] * d[i][j]) % cys;
for (int j = 0; j < m - k; j++) ans = ans * ((cys + 1) / 2) % cys;
for (int j = 0; j < n - k; j++) ans = ans * 2 % cys;
printf("%lld ", ans);
}
printf("\n");
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
const int MAXN = 53 + 5;
const int ha = 998244353;
const int inv2 = 499122177;
int n, m;
long long b[MAXN];
inline void insert(long long x) {
for (int i = m - 1; i >= 0; --i) {
if ((x >> i) & 1) {
if (b[i])
x ^= b[i];
else {
b[i] = x;
break;
}
}
}
}
int sz;
inline void add(int &x, int y) {
x += y;
if (x >= ha) x -= ha;
}
namespace Subtask1 {
std::vector<long long> v;
int ans[233];
inline void dfs(int step, long long now) {
if (step == sz) {
add(ans[__builtin_popcountll(now)], 1);
return;
}
dfs(step + 1, now);
dfs(step + 1, now ^ v[step]);
}
inline void Solve() {
for (int i = 0; i <= m - 1; ++i)
if (b[i]) v.push_back(b[i]);
dfs(0, 0);
int pw = 1;
for (int i = 1; i <= n - sz; ++i) pw = 2ll * pw % ha;
for (int i = 0; i <= m; ++i) printf("%lld ", 1ll * ans[i] * pw % ha);
puts("");
exit(0);
}
} // namespace Subtask1
inline int qpow(int a, int n = ha - 2) {
int res = 1;
while (n) {
if (n & 1) res = 1ll * res * a % ha;
a = 1ll * a * a % ha;
n >>= 1;
}
return res;
}
int fac[MAXN], inv[MAXN];
inline void prework() {
fac[0] = 1;
for (int i = 1; i <= MAXN - 1; ++i) fac[i] = 1ll * fac[i - 1] * i % ha;
inv[MAXN - 1] = qpow(fac[MAXN - 1]);
for (int i = MAXN - 2; i >= 0; --i) inv[i] = 1ll * inv[i + 1] * (i + 1) % ha;
}
inline int C(int n, int m) {
if (n < m) return 0;
if (n < 0 || m < 0) return 0;
return 1ll * fac[n] * inv[m] % ha * inv[n - m] % ha;
}
namespace Subtask2 {
long long B[MAXN];
int f[666], ans[233];
std::vector<long long> v;
inline void dfs(int step, long long now) {
if (step == m - sz) {
f[__builtin_popcountll(now)]++;
return;
}
dfs(step + 1, now);
dfs(step + 1, now ^ v[step]);
}
inline void Solve() {
for (int i = 0; i <= m - 1; ++i) {
for (int j = 0; j <= m - 1; ++j) {
B[j] |= ((b[i] >> j) & 1) << i;
}
}
for (int i = 0; i <= m - 1; ++i) {
B[i] ^= (1ll << i);
if ((B[i] >> i) & 1) v.push_back(B[i]);
}
dfs(0, 0);
for (int c = 0; c <= m; ++c) {
for (int d = 0; d <= m; ++d) {
int sm = 0;
for (int i = 0; i <= d; ++i) {
int gx = 1ll * C(d, i) * C(m - d, c - i) % ha;
if (i & 1) gx = ha - gx;
add(sm, gx);
}
sm = 1ll * sm * f[d] % ha;
add(ans[c], sm);
}
ans[c] = 1ll * ans[c] * qpow(inv2, m - sz) % ha;
ans[c] = 1ll * ans[c] * qpow(2, n - sz) % ha;
printf("%d ", ans[c]);
}
puts("");
exit(0);
}
} // namespace Subtask2
int main() {
prework();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
long long x;
scanf("%lld", &x);
insert(x);
}
for (int i = m - 1; i >= 0; --i) {
if (b[i]) {
for (int j = i + 1; j <= m - 1; ++j) {
if ((b[j] >> i) & 1) b[j] ^= b[i];
}
}
}
for (int i = 0; i <= m - 1; ++i)
if (b[i]) ++sz;
if (sz <= 26)
Subtask1::Solve();
else
Subtask2::Solve();
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int maxN = 223456;
const int P = 998244353;
int n, m, rnk;
i64 base[maxN], p[maxN], dp[60][60][2];
int cnt[maxN], ans[maxN];
void dfs1(int d, i64 x) {
if (d == rnk) {
cnt[__builtin_popcountll(x)]++;
} else {
dfs1(d + 1, x);
dfs1(d + 1, x ^ p[d]);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
i64 x;
scanf("%lld", &x);
for (int j = m - 1; j >= 0; j--) {
if (base[j]) {
x = min(x, base[j] ^ x);
} else if (x & (1ll << j)) {
base[j] = x;
p[rnk++] = x;
break;
}
}
}
if (rnk <= m / 2) {
dfs1(0, 0);
i64 multi = 1;
for (int i = 0; i < n - rnk; i++) multi = multi * 2 % P;
for (int i = 0; i <= m; i++) {
printf("%lld ", cnt[i] * multi % P);
}
} else {
for (int i = m - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--)
base[i] = min(base[i], base[i] ^ base[j]);
}
rnk = 0;
for (int i = 0; i < m; i++)
if (base[i] == 0) {
i64 x = (1ll << i);
for (int j = i + 1; j < m; j++)
if (base[j] & (1ll << i)) {
x ^= (1ll << j);
}
p[rnk++] = x;
}
dfs1(0, 0);
for (int x = 0; x <= m; x++) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int j = 0; j < m; j++)
for (int k = 0; k <= m; k++)
for (int par = 0; par <= 1; par++) {
dp[j + 1][k + 1][par ^ (j < x)] += dp[j][k][par];
dp[j + 1][k][par] += dp[j][k][par];
}
for (int k = 0; k <= m; k++) {
i64 w = dp[m][k][0] - dp[m][k][1];
w %= P;
if (w < 0) w += P;
ans[k] = (ans[k] + cnt[x] * w) % P;
}
}
i64 multi = 1;
for (int i = 0; i < n; i++) multi = multi * 2 % P;
for (int i = 0; i < m; i++) multi = multi * (P + 1) / 2 % P;
for (int i = 0; i <= m; i++) {
printf("%lld ", ans[i] * multi % P);
}
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m;
vector<long long> bs;
int cnt[(1 << 16) + 1];
long long inv2 = (998244353 + 1) / 2;
int popcount(long long x) { return __builtin_popcountll(x); }
bool add(long long x) {
for (auto& t : bs) x = min(x, t ^ x);
if (!x) return false;
for (int i = 0; i < ((int)(bs).size() + 1); ++i)
if (i == (int)(bs).size() || x > bs[i]) {
bs.insert(bs.begin() + i, x);
return true;
}
}
void build() {
for (int i = 0; i < (int)(bs).size(); i++) {
for (int j = i + 1; j < (int)(bs).size(); j++)
bs[i] = min(bs[i], bs[i] ^ bs[j]);
}
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
if (!(bs[i] >> l & 1)) {
int r = -1;
for (int j = l - 1; j >= 0; j--) {
if (bs[i] >> j & 1) {
r = j;
break;
}
}
for (int j = 0; j < (int)(bs).size(); j++) {
long long a = (bs[j] >> l & 1), b = (bs[j] >> r & 1);
if (a != b) bs[j] ^= (1LL << l) | (1LL << r);
}
}
}
while ((int)(bs).size() < m)
bs.push_back((1LL << (m - 1 - (int)(bs).size())));
for (int i = 0; i < (int)(bs).size(); i++) {
int l = m - 1 - i;
for (int j = l + 1; j < (int)(bs).size(); j++) {
bs[i] |= (bs[m - 1 - j] >> (m - 1 - i) & 1) << j;
}
}
}
long long cur = 0;
long long pk = 1;
long long res[55], fres[55];
long long C[55][55], f[55][55];
void dfs(int d) {
if (d == (int)(bs).size()) {
res[popcount(cur)]++;
return;
}
dfs(d + 1);
cur ^= bs[d];
dfs(d + 1);
cur ^= bs[d];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 1; i < (1 << 16); i++) {
cnt[i] = cnt[i / 2] + i % 2;
}
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
if (!add(x)) pk *= 2, pk %= mod;
}
if ((int)(bs).size() <= 27) {
dfs(0);
for (int i = 0; i <= m; i++) cout << (pk * res[i] % mod + mod) % mod << " ";
cout << "\n";
} else {
for (int i = 0; i < 55; i++) C[i][i] = C[i][0] = 1;
for (int i = 0; i < 55; i++) {
for (int j = 1; j < i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
for (int b = 0; b <= m; b++) {
for (int c = 0; c <= m; c++) {
for (int a = 0; a <= b && a <= c; a++) {
int s = (a % 2 == 0 ? 1 : -1);
f[b][c] = (f[b][c] + s * C[b][a] * C[m - b][c - a]) % mod;
}
}
}
int k = (int)(bs).size();
build();
dfs(k);
long long pmk = 1;
for (int i = 0; i < m - k; i++) pk = (pk * inv2) % mod;
for (int c = 0; c <= m; c++) {
for (int b = 0; b <= m; b++) {
fres[c] += (f[b][c] * res[b]);
fres[c] %= mod;
}
}
for (int i = 0; i <= m; i++)
cout << (pk * fres[i] % mod + mod) % mod << " ";
cout << "\n";
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int mod_inv(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return mod_inv(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
void generate_all() {
vector<int64_t> change(B, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
answers[0] = 1;
int64_t value = 0;
for (int64_t mask = 1; mask < 1LL << B; mask++) {
value ^= change[__builtin_ctzll(mask)];
answers[__builtin_popcountll(value)]++;
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++) {
int highest_bit = 63 - __builtin_clzll(basis[j]);
if (basis[i] >> highest_bit & 1) basis[i] ^= basis[j];
};
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
if (R <= 20) {
vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
} else {
vector<vector<unsigned>> dp(count_sum - 1, vector<unsigned>(1 << R, 0));
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = min(max_count - 1, count_sum - 1 - opt_count - 1);
count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
dp[opt_count - 1][opt_mask]++;
max_count += opt_count;
}
for (int count = 0; count < count_sum - 1; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + 1 + __builtin_popcount(mask)] += dp[count][mask];
int everything = 0;
for (int i = 0; i < B; i++) everything ^= options[i].second;
answers[count_sum + __builtin_popcount(everything)]++;
answers[0]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.57 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int M = 55;
const int mod = 998244353;
const int inv2 = (mod + 1) / 2;
inline int pls(int a, int b) {
a += b - mod;
return a + (a >> 31 & mod);
}
inline int mns(int a, int b) {
a -= b;
return a + (a >> 31 & mod);
}
inline void inc(int& a, int b) {
a += b - mod;
a += a >> 31 & mod;
}
inline void dec(int& a, int b) {
a -= b;
a += a >> 31 & mod;
}
inline int fpow(int b, int k) {
int res = 1;
while (k) {
if (k & 1) res = 1LL * res * b % mod;
b = 1LL * b * b % mod;
k >>= 1;
}
return res;
}
int _w;
int n, m, binom[M][M], W[M][M], k, ans[M];
long long A[M], B[M], f[M];
void dfs(int u, long long now) {
if (u == k) return ++ans[__builtin_popcountll(now)], void();
dfs(u + 1, now);
dfs(u + 1, now ^ A[u]);
}
int main(void) {
_w = scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
long long x;
_w = scanf("%lld", &x);
for (int j = m - 1; ~j; --j)
if ((x >> j) & 1) {
if (!f[j]) f[j] = x;
x ^= f[j];
}
}
for (int i = 0; i < m; ++i)
if (f[i]) {
for (int j = 0; j < i; ++j)
if ((f[i] >> j) & 1) f[i] ^= f[j];
for (int j = 0; j < m; ++j)
if (!f[j]) A[k] = A[k] * 2 + ((f[i] >> j) & 1);
A[k] |= 1LL << (m - k);
++k;
}
if (k <= 26) {
dfs(0, 0);
int tmp = fpow(2, n - k);
for (int i = 0; i <= m; ++i) printf("%lld ", 1LL * ans[i] * tmp % mod);
return 0;
}
swap(A, B), k = m - k;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < m - k; ++j)
if ((B[j] >> i) & 1) A[i] |= 1LL << j;
A[i] |= 1LL << (m - 1 - i);
}
dfs(0, 0);
for (int i = 0; i <= m; ++i) {
binom[i][0] = 1;
for (int j = 1; j <= i; ++j)
binom[i][j] = pls(binom[i - 1][j], binom[i - 1][j - 1]);
}
for (int i = 0; i <= m; ++i)
for (int j = 0; j <= m; ++j)
for (int k = 0; k <= i; ++k)
if (k & 1)
dec(W[i][j], 1LL * binom[j][k] * binom[m - j][i - k] % mod);
else
inc(W[i][j], 1LL * binom[j][k] * binom[m - j][i - k] % mod);
int res;
for (int i = 0; i <= m; ++i) {
res = 0;
for (int j = 0; j <= m; ++j) inc(res, 1LL * W[i][j] * ans[j] % mod);
printf("%lld ", 1LL * fpow(2, (n - m + mod - 1) % (mod - 1)) * res % mod);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return x;
}
const int Mod = 998244353, inv2 = Mod + 1 >> 1, MAXW = (1 << 18) - 1;
int upd(int x) { return x + (x >> 31 & Mod); }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = 1LL * x * x % Mod)
if (y & 1) res = 1LL * res * x % Mod;
return res;
}
int n, m;
long long w[200010], lb[60], Atot;
int popcount[1 << 18];
long long bin[60];
int bintop;
int p[60], q[60], ans[60];
int ins(long long x) {
for (int i = m - 1; i >= 0; i--)
if (x >> i & 1) {
if (lb[i]) {
x ^= lb[i];
continue;
}
lb[i] = x;
for (int j = i - 1; j >= 0; j--)
if (lb[i] >> j & 1) lb[i] ^= lb[j];
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) lb[j] ^= lb[i];
return 1;
}
return 0;
}
void dfs(int x, long long msk, int *p) {
if (x > bintop)
return p[popcount[msk & MAXW] + popcount[msk >> 18 & MAXW] +
popcount[msk >> 36 & MAXW]]++,
void();
dfs(x + 1, msk, p);
dfs(x + 1, msk ^ bin[x], p);
}
int dp[60][60];
int C[60][60];
int main() {
for (int i = 1; i <= MAXW; i++) popcount[i] = popcount[i >> 1] + (i & 1);
n = read(), m = read();
for (int i = 1; i <= n; i++) Atot += ins(w[i] = read());
if (Atot <= 26) {
for (int i = 0; i < m; i++)
if (lb[i]) bin[++bintop] = lb[i];
dfs(1, 0, p);
int t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
for (int i = 0; i < m; i++)
if (!lb[i]) {
long long x = 1LL << i;
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) x |= 1LL << j;
bin[++bintop] = x;
}
dfs(1, 0, q);
for (int i = 0; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
for (int k = 0, t = 1; k <= i && k <= j; k++, t = Mod - t) {
dp[i][j] = (dp[i][j] + 1LL * t * C[j][k] % Mod * C[m - j][i - k]) % Mod;
}
}
for (int c = 0; c <= m; c++)
for (int i = 0; i <= m; i++) p[c] = (p[c] + 1LL * q[i] * dp[c][i]) % Mod;
int t = qpow(inv2, m - Atot);
for (int i = 0; i <= m; i++) p[i] = 1LL * p[i] * t % Mod;
t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
void row_reduce(vector<int64_t> &basis) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
}
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> counts(M + 1, 0);
int64_t value = 0;
for (int64_t mask = 0; mask < 1LL << B; mask++) {
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctzll(mask + 1)];
}
return counts;
}
vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int> highest_bit(B, -1);
vector<bool> bit_covered(M, false);
for (int i = 0; i < B; i++) {
highest_bit[i] = 63 - __builtin_clzll(basis[i]);
bit_covered[highest_bit[i]] = true;
}
vector<int64_t> orthogonal;
for (int bit = M - 1; bit >= 0; bit--)
if (!bit_covered[bit]) {
int64_t value = 1LL << bit;
for (int i = 0; i < B; i++)
value |= (basis[i] >> bit & 1) << highest_bit[i];
orthogonal.push_back(value);
}
return orthogonal;
}
vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> orthogonal = build_orthogonal(basis, M);
vector<int64_t> orthogonal_counts = generate_all(orthogonal, M);
vector<int64_t> counts(M + 1, 0);
vector<vector<int64_t>> choose(M + 1);
for (int n = 0; n <= M; n++) {
choose[n].resize(n + 1);
choose[n][0] = choose[n][n] = 1;
for (int r = 1; r < n; r++)
choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
}
auto &&get_choose = [&](int n, int r) -> int64_t {
if (r < 0 || r > n) return 0;
return choose[n][r];
};
for (int c = 0; c <= M; c++) {
vector<int64_t> w(M + 1, 0);
for (int d = 0; d <= M; d++)
for (int j = 0; j <= d; j++)
w[d] +=
(j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j);
for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d];
assert((counts[c] & ((1LL << (M - B)) - 1)) == 0);
counts[c] >>= M - B;
}
return counts;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
vector<int64_t> answers;
if (B <= M / 2)
answers = generate_all(basis, M);
else
answers = generate_from_orthogonal(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace mine {
long long qread() {
long long ans = 0, f = 1;
char c = getchar();
while (c < '0' or c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
return ans * f;
}
void write(long long num) {
if (num < 0) putchar('-'), num = -num;
if (num >= 10) write(num / 10);
putchar('0' + num % 10);
}
void write1(long long num) {
write(num);
putchar(' ');
}
void write2(long long num) {
write(num);
putchar('\n');
}
template <typename T>
inline bool chmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
bool IN(long long x, long long l, long long r) { return l <= x and x <= r; }
void GG() {
puts("-1");
exit(0);
}
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
long long ans = 1;
while (e) {
if (e & 1) ans = ans * x % mod;
x = x * x % mod;
e >>= 1;
}
return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int MM = 5e6 + 10;
long long fac[MM], facinv[MM], Inv[MM];
long long Comb(int n, int m) {
return n < 0 or m < 0 or n < m
? 0
: fac[n] * facinv[m] % MOD * facinv[n - m] % MOD;
}
void PRE() {
fac[0] = 1;
for (int i = (1), I = (MM - 1); i <= I; i++) fac[i] = fac[i - 1] * i % MOD;
facinv[MM - 1] = invm(fac[MM - 1]);
for (int i = (MM - 1), I = (1); i >= I; i--)
facinv[i - 1] = facinv[i] * i % MOD;
Inv[1] = 1;
for (int i = (2), I = (MM - 1); i <= I; i++)
Inv[i] = (MOD - MOD / i) * Inv[MOD % i] % MOD;
}
const int N = 1e6 + 10;
namespace BS {
long long bs[100];
int cnt, zero;
void clear() {
memset(bs, 0, sizeof bs);
cnt = zero = 0;
}
void insert(long long num) {
for (int i = (60), I = (0); i >= I; i--)
if (num & (1ll << (i))) {
if (!bs[i]) {
for (int t = (0), I = (i - 1); t <= I; t++)
if (num & (1ll << (t))) num ^= bs[t];
for (int t = (i + 1), I = (60); t <= I; t++)
if (bs[t] & (1ll << (i))) bs[t] ^= num;
bs[i] = num, cnt++;
return;
} else
num ^= bs[i];
}
zero++;
}
long long findkth(long long k) {
long long ret = 0, right = cnt;
k -= (zero > 0);
for (int i = (60), I = (0); i >= I; i--)
if (bs[i]) {
right--;
if (k & (1ll << (right))) ret ^= bs[i], k -= (1ll << (right));
}
return k ? -1 : ret;
}
long long findrk(long long num) {
long long rk = 0, right = cnt;
for (int i = (60), I = (0); i >= I; i--)
if (bs[i]) {
right--;
if (num & (1ll << (i))) rk |= (1ll << (right));
}
return rk;
}
}; // namespace BS
vector<long long> can;
int tim[100];
void dfs(int i, long long now) {
if (i == ((int)(can).size()))
tim[__builtin_popcountll(now)]++;
else
dfs(i + 1, now), dfs(i + 1, now ^ can[i]);
}
void main() {
int n = qread(), m = qread();
for (int i = (1), I = (n); i <= I; i++) BS::insert(qread());
int B = BS::cnt;
if (B <= 27) {
for (int i = (0), I = (60); i <= I; i++)
if (BS::bs[i]) can.push_back(BS::bs[i]);
dfs(0, 0);
for (int L = (0), I = (m); L <= I; L++)
write1(tim[L] * qpower(2, n - B) % MOD);
} else {
PRE();
long long pre[100][100];
memset(pre, 0, sizeof pre);
for (int L = (0), I = (m); L <= I; L++)
for (int a = (0), I = (m); a <= I; a++)
for (int k = (0), I = (a); k <= I; k++)
add(pre[L][a], (k & 1 ? MOD - 1 : 1) * Comb(a, k) % MOD *
Comb(m - a, L - k) % MOD);
for (int i = (0), I = (m - 1); i <= I; i++)
if (!BS::bs[i]) {
long long A = (1ll << (i));
for (int j = (0), I = (m - 1); j <= I; j++)
A |= (BS::bs[j] >> i) % 2 * (1ll << (j));
can.push_back(A);
}
dfs(0, 0);
for (int L = (0), I = (m); L <= I; L++) {
long long sum = 0;
for (int a = (0), I = (m); a <= I; a++)
add(sum, tim[a] * pre[L][a] % MOD);
write1(sum * qpower(2, n + MOD - 1 - m) % MOD);
}
}
}
}; // namespace mine
signed main() {
srand(time(0));
mine::main();
printf("");
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const long long MOD1 = 2286661337;
const long long MOD2 = 998244353;
const int INF = (int)1e9 + 7;
const double EPS = 1e-7;
const int N = (int)2e5;
const int M = 53;
int n, m, k;
long long a[N], b[M], in_basis[M], p[M + 1], q[M + 1], cnk[M + 1][M + 1],
w[M + 1][M + 1], S[1 << 25];
int popcnt(long long x) {
bitset<64> h(x);
return h.count();
}
void solve1() {
q[0] = 1;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < 1 << i; ++j) {
++q[popcnt(S[j] ^ a[i])];
if (i != k - 1) {
S[(1 << i) + j] = S[j] ^ a[i];
}
}
}
for (int i = 0; i < m + 1; ++i) {
p[i] = q[i];
}
long long pow_ = 1;
for (int i = 0; i < n - k; ++i) {
pow_ = pow_ * 2 % MOD2;
}
for (int i = 0; i < m + 1; ++i) {
p[i] = p[i] * pow_ % MOD2;
}
}
void make_cnk() {
for (int n = 0; n < m + 1; ++n) {
for (int k = 0; k < m + 1; ++k) {
if (n == 0) {
cnk[n][k] = (k == 0);
} else {
if (k == 0) {
cnk[n][k] = 1;
} else {
cnk[n][k] = (cnk[n - 1][k] + cnk[n - 1][k - 1]) % MOD2;
}
}
}
}
}
void make_w() {
for (int d = 0; d < m + 1; ++d) {
for (int c = 0; c < m + 1; ++c) {
for (int pow_ = 0; pow_ < c + 1; ++pow_) {
long long cur = ((pow_ & 1) ? MOD2 - 1 : 1);
cur = cur * cnk[d][pow_] % MOD2;
cur = cur * cnk[m - d][c - pow_] % MOD2;
w[c][d] = (w[c][d] + cur) % MOD2;
}
}
}
}
long long bin_pow(long long x, long long p) {
if (p == 0) {
return 1;
}
long long res = bin_pow(x, p >> 1);
res = res * res % MOD2;
if (p & 1) {
res = res * x % MOD2;
}
return res;
}
void solve2() {
for (int i = 0; i < m - k; ++i) {
int ind = 0;
int ind_a = 0;
for (int j = m - 1; j >= 0; --j) {
if (!in_basis[j]) {
if (ind == i) {
ind_a = j;
break;
}
++ind;
}
}
ind = 0;
int ind_b = 0;
for (int j = m - 1; j >= 0; --j) {
if (in_basis[j]) {
if (a[ind_b] >> ind_a & 1) {
b[i] |= 1ll << j;
}
++ind_b;
} else {
if (ind == i) {
b[i] |= 1ll << j;
}
++ind;
}
}
}
q[0] = 1;
for (int i = 0; i < m - k; ++i) {
for (int j = 0; j < 1 << i; ++j) {
++q[popcnt(S[j] ^ b[i])];
if (i != m - k - 1) {
S[(1 << i) + j] = S[j] ^ b[i];
}
}
}
make_cnk();
make_w();
long long pow_ = 1;
for (int i = 0; i < m - k; ++i) {
pow_ = pow_ * 2 % MOD2;
}
pow_ = bin_pow(pow_, MOD2 - 2);
for (int i = 0; i < m + 1; ++i) {
for (int d = 0; d < m + 1; ++d) {
p[i] = (p[i] + w[i][d] * q[d]) % MOD2;
}
p[i] = p[i] * pow_ % MOD2;
}
pow_ = 1;
for (int i = 0; i < n - k; ++i) {
pow_ = pow_ * 2 % MOD2;
}
for (int i = 0; i < m + 1; ++i) {
p[i] = p[i] * pow_ % MOD2;
}
}
signed main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
}
for (int i = 0; i < n; ++i) {
for (int j = m - 1; j >= 0; --j) {
if (a[i] >> j & 1) {
if (in_basis[j]) {
a[i] ^= in_basis[j];
} else {
in_basis[j] = a[i];
break;
}
}
}
}
for (int i = m - 1; i >= 0; --i) {
if (in_basis[i]) {
a[k] = in_basis[i];
for (int j = 0; j < k; ++j) {
if (a[j] >> i & 1) {
a[j] ^= a[k];
}
}
++k;
}
}
if (k <= m / 2) {
solve1();
} else {
solve2();
}
for (int i = 0; i < m + 1; ++i) {
printf("%lld ", p[i]);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m;
long long P[55], B[55], T[55], Comb[55][55];
void Put(long long a) {
int i;
for (i = m; i >= 0; i--) {
if ((a >> i) & 1) {
if (!P[i]) {
P[i] = a;
return;
}
a ^= P[i];
}
}
}
int C[55], Mod = 998244353;
void DFS(int pv, long long x) {
if (pv == -1) {
C[__builtin_popcountll(x)]++;
return;
}
DFS(pv - 1, x);
DFS(pv - 1, x ^ T[pv]);
}
int main() {
int i, j, k, c1 = 0, c2 = 0;
scanf("%d%d", &n, &m);
long long a;
for (i = 0; i < n; i++) {
scanf("%lld", &a);
Put(a);
}
for (i = 0; i < m; i++) {
if (!P[i]) continue;
for (j = 0; j < i; j++) {
if (P[j] && ((P[i] >> j) & 1)) P[i] ^= P[j];
}
T[c1++] = P[i];
}
long long off = 1, Inv2 = Mod / 2 + 1;
for (i = 0; i < n - c1; i++) off = off * 2 % Mod;
if (c1 <= 27) {
DFS(c1 - 1, 0);
for (i = 0; i <= m; i++) printf("%lld ", C[i] * off % Mod);
return 0;
}
for (i = 0; i < m; i++) {
if (P[i]) continue;
long long t = (1ll << i);
for (j = 0; j < m; j++) {
if (P[j] & (1ll << i)) t |= (1ll << j);
}
T[c2++] = t;
}
off = 1;
for (i = 0; i < n; i++) off = off * 2 % Mod;
for (i = 0; i < m; i++) off = off * Inv2 % Mod;
DFS(c2 - 1, 0);
for (i = 0; i <= m; i++) {
Comb[i][0] = 1;
for (j = 1; j <= i; j++)
Comb[i][j] = (Comb[i - 1][j - 1] + Comb[i - 1][j]) % Mod;
}
for (i = 0; i <= m; i++) {
long long rr = 0;
for (j = 0; j <= m; j++) {
long long s = 0;
for (k = 0; k <= min(i, j); k++) {
int t = Comb[j][k] * Comb[m - j][i - k] % Mod;
if (k & 1)
s -= t;
else
s += t;
}
s = s % Mod + Mod;
rr = (rr + C[j] * s) % Mod;
}
printf("%lld ", rr * off % Mod);
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, num = 0, cnt[300010];
long long a, b[110], c[110], sum[110], pw2[200010], C[110][110];
int getcnt(long long x) {
return cnt[x & ((1 << 18) - 1)] + cnt[(x >> 18) & ((1 << 18) - 1)] +
cnt[x >> 36];
}
long long power(long long a, int b) {
long long ans = 1;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1) ans = ans * a % mod;
return ans;
}
void dfs1(int s, long long cur) {
if (s == m) return (sum[getcnt(cur)] += pw2[n - num]) %= mod, void();
dfs1(s + 1, cur);
if (b[s]) dfs1(s + 1, cur ^ b[s]);
}
void dfs2(int s, long long cur) {
if (s == num) return (sum[getcnt(cur)] += pw2[n - num]) %= mod, void();
dfs2(s + 1, cur);
dfs2(s + 1, cur ^ c[s]);
}
int main() {
for (int i = 1; i < (1 << 18); i++) cnt[i] = cnt[i ^ (i & (-i))] + 1;
cin >> n >> m;
pw2[0] = 1;
for (int i = 1; i <= n; i++) pw2[i] = pw2[i - 1] * 2 % mod;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a);
for (int j = m - 1; j >= 0; j--)
if ((a >> j) & 1)
if (b[j])
a ^= b[j];
else {
b[j] = a;
num++;
break;
}
}
if (num <= 26) {
dfs1(0, 0);
for (int i = 0; i <= m; i++) printf("%lld ", sum[i]);
} else {
for (int i = m - 1; i >= 0; i--)
if (b[i])
for (int j = i + 1; j < m; j++)
if ((b[j] >> i) & 1) b[j] ^= b[i];
num = 0;
for (int i = 0; i < m; i++)
if (!b[i]) c[num++] ^= (1ll << i);
for (int i = 0; i < m; i++)
if (b[i]) {
int t = 0;
for (int j = 0; j < m; j++)
if (!b[j]) c[t++] ^= (((b[i] >> j) & 1) << i);
}
dfs2(0, 0);
C[0][0] = 1;
for (int i = 1; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
for (int i = 0; i <= m; i++) {
long long ans = 0;
for (int j = 0; j <= m; j++)
for (int k = 0; k <= i && k <= j; k++)
(ans += (k & 1 ? mod - 1 : 1) * sum[j] % mod * C[j][k] % mod *
C[m - j][i - k]) %= mod;
printf("%lld ", ans * power(pw2[m - num], mod - 2) % mod);
}
}
puts("");
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int mod_inv(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return mod_inv(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
const int POPCOUNT_BITS = 14;
const int POPCOUNT_MASK = (1 << POPCOUNT_BITS) - 1;
vector<int8_t> _popcount;
void build_popcount() {
_popcount.resize(1 << POPCOUNT_BITS);
for (int x = 0; x < 1 << POPCOUNT_BITS; x++)
_popcount[x] = _popcount[x >> 1] + (x & 1);
}
int popcountll(int64_t x) {
return _popcount[x & POPCOUNT_MASK] +
_popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] +
_popcount[x >> (2 * POPCOUNT_BITS) & POPCOUNT_MASK] +
_popcount[x >> (3 * POPCOUNT_BITS)];
}
int popcount(int x) {
return _popcount[x & POPCOUNT_MASK] +
_popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] +
_popcount[x >> (2 * POPCOUNT_BITS)];
}
void generate_all() {
vector<int64_t> change(B, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
answers[0] = 1;
int64_t value = 0;
for (int64_t mask = 1; mask < 1LL << B; mask++) {
value ^= change[__builtin_ctzll(mask)];
answers[popcountll(value)]++;
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
;
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
if (R <= 20) {
vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
} else {
vector<vector<unsigned>> dp(count_sum - 1, vector<unsigned>(1 << R, 0));
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = min(max_count - 1, count_sum - 1 - opt_count - 1);
count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
dp[opt_count - 1][opt_mask]++;
max_count += opt_count;
}
for (int count = 0; count < count_sum - 1; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + 1 + __builtin_popcount(mask)] += dp[count][mask];
int everything = 0;
for (int i = 0; i < B; i++) everything ^= options[i].second;
answers[count_sum + __builtin_popcount(everything)]++;
answers[0]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
build_popcount();
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.57 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
int pw2(int y) {
if (y < 0) y += P - 1;
int x = 2;
int s = 1;
for (; y; y >>= 1, x = (long long)x * x % P)
if (y & 1) s = (long long)s * x % P;
return s;
}
int k, c[100];
long long a[100];
void calc(int t, long long x) {
if (t > k) {
++c[__builtin_popcountll(x)];
return;
}
calc(t + 1, x), calc(t + 1, x ^ a[t]);
}
int m, C[100][100];
long long bs[100];
bool ins(long long x) {
for (int i = m - 1, iend = 0; i >= iend; --i)
if (x >> i & 1) {
if (bs[i]) {
if (!(x ^= bs[i])) return false;
} else {
for (int j = i - 1, jend = 0; j >= jend; --j)
if (bs[j] && (x & (1LL << j))) x ^= bs[j];
bs[i] = x;
for (int j = i + 1, jend = m - 1; j <= jend; ++j)
if (bs[j] && (bs[j] & (1LL << i))) bs[j] ^= bs[i];
return true;
}
}
return false;
}
int main() {
int n, b = 0;
scanf("%d%d", &n, &m);
for (int i = 1, iend = n; i <= iend; ++i) {
long long x;
scanf("%lld", &x);
b += ins(x);
}
if (b <= m / 2) {
for (int i = 0, iend = m - 1; i <= iend; ++i)
if (bs[i]) a[++k] = bs[i];
calc(1, 0);
for (int i = 0, iend = m; i <= iend; ++i)
printf("%d ", ((long long)c[i] * pw2(n - k)) % P);
} else {
for (int i = 0, iend = m - 1; i <= iend; ++i)
if (!bs[i]) {
int t = 0;
++k;
for (int j = 0, jend = m - 1; j <= jend; ++j)
if (bs[j]) {
a[k] |= (bs[j] >> i & 1) << t;
++t;
}
a[k] |= 1LL << (b + k);
}
calc(1, 0);
for (int i = 0, iend = m; i <= iend; ++i) {
C[i][0] = 1;
for (int j = 1, jend = i; j <= jend; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P;
}
for (int i = 0, iend = m; i <= iend; ++i) {
int tot = 0;
for (int j = 0, jend = m; j <= jend; ++j) {
int s = 0;
for (int k = 0, kend = i; k <= kend; ++k) {
int x = (long long)C[j][k] * C[m - j][i - k] % P;
if (k & 1)
s = (s + P - x) % P;
else
s = (s + x) % P;
}
tot = (tot + (long long)s * c[j]) % P;
}
printf("%d ", (long long)tot * pw2(n - m) % P);
}
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <int MOD_>
struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) {
if (v < 0) v += MOD;
}
explicit operator int() const { return v; }
friend std::ostream& operator<<(std::ostream& out, const modnum& n) {
return out << int(n);
}
friend std::istream& operator>>(std::istream& in, modnum& n) {
ll v_;
in >> v_;
n = modnum(v_);
return in;
}
friend bool operator==(const modnum& a, const modnum& b) {
return a.v == b.v;
}
friend bool operator!=(const modnum& a, const modnum& b) {
return a.v != b.v;
}
modnum inv() const {
modnum res;
res.v = minv(v, MOD);
return res;
}
friend modnum inv(const modnum& m) { return m.inv(); }
modnum neg() const {
modnum res;
res.v = v ? MOD - v : 0;
return res;
}
friend modnum neg(const modnum& m) { return m.neg(); }
modnum operator-() const { return neg(); }
modnum operator+() const { return modnum(*this); }
modnum& operator++() {
v++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator--() {
if (v == 0) v = MOD;
v--;
return *this;
}
modnum& operator+=(const modnum& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modnum& operator-=(const modnum& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modnum& operator*=(const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum& operator/=(const modnum& o) { return *this *= o.inv(); }
friend modnum operator++(modnum& a, int) {
modnum r = a;
++a;
return r;
}
friend modnum operator--(modnum& a, int) {
modnum r = a;
--a;
return r;
}
friend modnum operator+(const modnum& a, const modnum& b) {
return modnum(a) += b;
}
friend modnum operator-(const modnum& a, const modnum& b) {
return modnum(a) -= b;
}
friend modnum operator*(const modnum& a, const modnum& b) {
return modnum(a) *= b;
}
friend modnum operator/(const modnum& a, const modnum& b) {
return modnum(a) /= b;
}
};
using num = modnum<998244353>;
template <typename T>
T pow(T a, long long b) {
assert(b >= 0);
T r = 1;
while (b) {
if (b & 1) r *= a;
b >>= 1;
a *= a;
}
return r;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int N;
cin >> N;
int M;
cin >> M;
int numExtra = 0;
vector<int64_t> basis;
basis.reserve(M);
for (int z = 0; z < N; z++) {
int64_t a;
cin >> a;
for (int64_t x : basis) {
a = min(a, a ^ x);
}
if (a) {
for (int64_t& x : basis) {
x = min(x, a ^ x);
}
basis.push_back(a);
} else {
numExtra++;
}
}
auto getFrequency = [&](const vector<int64_t>& b) -> vector<num> {
vector<int> res(M + 1);
int64_t cur = 0;
for (int z = 0; z < (1 << int(b.size())); z++) {
if (z) {
cur ^= b[__builtin_ctz(z)];
}
res[__builtin_popcountll(cur)]++;
}
vector<num> numRes(M + 1);
for (int i = 0; i <= M; i++) numRes[i] = res[i];
return numRes;
};
int K = int(basis.size());
int L = M - K;
vector<num> ans;
if (K <= L) {
ans = getFrequency(basis);
} else {
vector<int64_t> perpBasis;
perpBasis.reserve(L);
for (int i = 0; i < M; i++) {
int64_t val = int64_t(1) << i;
for (int64_t a : basis) {
int d = 63 - __builtin_clzll(a);
if (i == d) {
val = 0;
break;
} else if (a & (int64_t(1) << i)) {
val |= int64_t(1) << d;
}
}
if (val) perpBasis.push_back(val);
}
vector<num> freq = getFrequency(perpBasis);
vector<num> fact(M + 1);
fact[0] = 1;
for (int i = 1; i <= M; i++) fact[i] = fact[i - 1] * num(i);
vector<num> ifact(M + 1);
ifact[M] = inv(fact[M]);
for (int i = M; i >= 1; i--) ifact[i - 1] = ifact[i] * num(i);
auto choose = [&fact, &ifact](int n, int r) -> num {
if (0 <= r && r <= n)
return fact[n] * ifact[n - r] * ifact[r];
else
return num(0);
};
ans.resize(M + 1);
num invPerpSz = pow(inv(num(2)), int(perpBasis.size()));
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= M; j++) {
num numOrtho = 0;
for (int k = 0; k <= j; k += 2) {
numOrtho += choose(j, k) * choose(M - j, i - k);
}
num weight = (2 * numOrtho - choose(M, i)) * invPerpSz;
ans[i] += weight * freq[j];
}
}
}
num extraFac = pow(num(2), numExtra);
for (int i = 0; i <= M; i++) {
cout << ans[i] * extraFac << " \n"[i == M];
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using std::max;
using std::min;
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3f;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return !b ? a : gcd(b, a % b);
}
template <typename _Tp>
inline _Tp abs(const _Tp &a) {
return a >= 0 ? a : -a;
}
template <typename _Tp>
inline void chmax(_Tp &a, const _Tp &b) {
a = a < b ? b : a;
}
template <typename _Tp>
inline void chmin(_Tp &a, const _Tp &b) {
a = a < b ? a : b;
}
template <typename _Tp>
inline void read(_Tp &x) {
char ch(getchar());
bool f(false);
while (!isdigit(ch)) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
f && (x = -x);
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
inline int read_str(char *s) {
char ch(getchar());
while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
char *tar = s;
*tar++ = ch, ch = getchar();
while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
*tar++ = ch, ch = getchar();
return *tar = 0, tar - s;
}
const int N = 200005;
const int mod = 998244353;
long long ksm(long long a, long long b = mod - 2) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod, b >>= 1;
}
return res;
}
template <typename _Tp1, typename _Tp2>
inline void add(_Tp1 &a, _Tp2 b) {
(a += b) >= mod && (a -= mod);
}
template <typename _Tp1, typename _Tp2>
inline void sub(_Tp1 &a, _Tp2 b) {
(a -= b) < 0 && (a += mod);
}
long long d[55], a[55], S, val[55];
int cnt[1 << 14], SIZ, ans[55], res[55], C[55][55];
inline int popc(long long x) {
return cnt[x & 0x3fff] + cnt[(x >> 14) & 0x3fff] + cnt[(x >> 28) & 0x3fff] +
cnt[(x >> 42) & 0x3fff];
}
void dfs(int dep, long long s) {
if (dep == SIZ) return ++ans[popc(s)], void();
dfs(dep + 1, s), dfs(dep + 1, s ^ a[dep]);
}
void DFS(int dep, long long s, int c) {
if (dep == SIZ) return ++res[popc(s) + c], void();
DFS(dep + 1, s, c), DFS(dep + 1, s ^ val[dep], c + 1);
}
int main() {
for (int i = 0; i < 55; ++i)
for (int j = C[i][0] = 1; j <= i; ++j)
C[i][j] = C[i - 1][j - 1], add(C[i][j], C[i - 1][j]);
for (int i = 0; i < 1 << 14; ++i) cnt[i] = cnt[i >> 1] + (i & 1);
int n, m;
read(n, m);
long long x;
for (int i = 1; i <= n; ++i) {
read(x);
for (int j = m - 1; j >= 0; --j)
if (x >> j & 1) {
if (d[j])
x ^= d[j];
else {
d[j] = x;
break;
}
}
}
for (int i = m - 1; i >= 0; --i)
if (d[i])
for (int j = i + 1; j < m; ++j)
if (d[j] >> i & 1) d[j] ^= d[i];
std::vector<long long> v;
for (int i = 0; i < m; ++i)
if (d[i]) v.push_back(d[i]);
long long mul = ksm(2, n - ((int)v.size()));
if (((int)v.size()) <= m - ((int)v.size())) {
for (int i = 0; i < ((int)v.size()); ++i) a[i] = v[i];
SIZ = ((int)v.size()), dfs(0, 0);
for (int i = 0; i <= m; ++i)
printf("%lld%c", mul * ans[i] % mod, " \n"[i == m]);
return 0;
}
SIZ = 0;
for (int i = 0; i < m; ++i)
if (!d[i]) {
for (int j = 0; j < ((int)v.size()); ++j)
if (v[j] >> i & 1) val[SIZ] |= 1LL << j;
++SIZ;
}
DFS(0, 0, 0);
for (int a = 0; a <= m; ++a)
for (int b = 0; b <= m; ++b) {
int coef = 0;
for (int i = 0; i <= a && i <= b; ++i) {
int tmp = 1LL * C[a][i] * C[m - a][b - i] % mod;
i & 1 ? sub(coef, tmp) : add(coef, tmp);
}
add(ans[b], 1LL * res[a] * coef % mod);
}
mul = mul * ksm((mod + 1) / 2, m - ((int)v.size())) % mod;
for (int i = 0; i <= m; ++i)
printf("%lld%c", mul * ans[i] % mod, " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
void generate_all() {
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
int64_t value = 0;
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
;
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
if (R <= 20) {
vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
} else {
vector<vector<unsigned>> dp(count_sum - 1, vector<unsigned>(1 << R, 0));
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = min(max_count - 1, count_sum - 1 - opt_count - 1);
count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
dp[opt_count - 1][opt_mask]++;
max_count += opt_count;
}
for (int count = 0; count < count_sum - 1; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + 1 + __builtin_popcount(mask)] += dp[count][mask];
int everything = 0;
for (int i = 0; i < B; i++) everything ^= options[i].second;
answers[count_sum + __builtin_popcount(everything)]++;
answers[0]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.57 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
template <typename T>
void maxtt(T &t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T &t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "RDLU";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int &x, int y, int mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
long long a[200135];
struct lsp {
long long a[60] = {0};
const int maxBit = 54;
bool insert(long long x) {
for (int i = maxBit; ~i; i--)
if (x & (1LL << i)) {
if (a[i] != 0)
x ^= a[i];
else {
for (int(j) = 0; (j) < (int)(i); (j)++)
if (x & (1LL << j)) x ^= a[j];
for (int j = i + 1; j <= maxBit; j++)
if (a[j] & (1LL << i)) a[j] ^= x;
a[i] = x;
return 1;
}
}
return 0;
}
lsp getOrthogonal(int m) {
lsp res;
vector<int> vp;
for (int j = m - 1; j >= 0; j--)
if (!a[j]) {
vp.push_back(j);
res.a[j] |= 1LL << j;
}
for (int j = m - 1; j >= 0; j--)
if (a[j]) {
int cc = 0;
for (int z = m - 1; z >= 0; z--)
if (!a[z]) {
long long w = (a[j] >> z) & 1;
res.a[vp[cc]] |= w << j;
cc++;
}
}
return res;
}
} sp;
int p[66], q[66];
void ppt() {
for (int i = 0; i <= m; i++) {
printf("%lld ", (long long)p[i] * fastPow(2, n - k) % 998244353);
}
exit(0);
}
vector<long long> bs;
inline void dfs(int *p, int k, int i, long long x) {
if (i == k) {
p[__builtin_popcountll(x)]++;
return;
}
dfs(p, k, i + 1, x);
dfs(p, k, i + 1, x ^ bs[i]);
}
void calsm(lsp sp, int *p, int k) {
bs.clear();
for (int(j) = 0; (j) < (int)(sp.maxBit); (j)++)
if (sp.a[j]) bs.push_back(sp.a[j]);
assert(bs.size() == k);
dfs(p, k, 0, 0);
}
void calbg() {
vector<vector<int>> C(60, vector<int>(60, 0));
C[0][0] = 1;
for (int i = 1; i <= 55; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 998244353;
}
}
vector<vector<int>> w(m + 1, vector<int>(m + 1, 0));
for (int c = 0; c <= m; c++) {
for (int d = 0; d <= m; d++) {
for (int j = 0; j <= c; j++) {
int val = (long long)C[d][j] * C[m - d][c - j] % 998244353;
if (j % 2 == 0)
addmod(w[c][d], val);
else
addmod(w[c][d], 998244353 - val);
}
}
}
lsp B = sp.getOrthogonal(m);
calsm(B, q, m - k);
for (int(c) = 0; (c) < (int)(m + 1); (c)++) {
for (int d = 0; d <= m; d++) {
addmod(p[c], (long long)q[d] * w[c][d] % 998244353);
}
}
int tmp = fastPow(2, m - k);
tmp = fastPow(tmp, 998244353 - 2);
for (int(c) = 0; (c) < (int)(m + 1); (c)++)
p[c] = (long long)p[c] * tmp % 998244353;
ppt();
}
void fmain(int tid) {
scanf("%d%d", &n, &m);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%lld", a + i);
for (int(i) = 1; (i) <= (int)(n); (i)++) k += sp.insert(a[i]);
if (k <= m / 2) {
calsm(sp, p, k);
ppt();
}
calbg();
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) {
if (ch == '-') f = -1;
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - 48;
}
return x * f;
}
const int mxN = 1 << 19;
const int mxM = 53;
const int P = 998244353;
long long comb[mxM + 3][mxM + 3];
int bitcnt[mxN + 3];
long long a[mxM + 3];
int b[mxM + 3];
long long ans[mxM + 3];
int n, m, sz;
long long quickpow(long long x, long long y) {
long long cur = x, ret = 1ll;
for (int i = 0; y; i++) {
if (y & (1ll << i)) {
y -= (1ll << i);
ret = ret * cur % P;
}
cur = cur * cur % P;
}
return ret;
}
void initfact(int n) {
comb[0][0] = 1ll;
for (int i = 1; i <= n; i++) {
comb[i][0] = comb[i][i] = 1ll;
for (int j = 1; j < i; j++) {
comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % P;
}
}
}
int getbitcnt(long long x) {
return bitcnt[x & 524287] + bitcnt[(x >> 19) & 524287] +
bitcnt[(x >> 38) & 524287];
}
namespace Solve1 {
void dfs(int pos, long long x) {
if (pos == sz) {
ans[getbitcnt(x)]++;
return;
}
dfs(pos + 1, x);
dfs(pos + 1, x ^ a[b[pos]]);
}
} // namespace Solve1
namespace Solve2 {
long long sta[mxM + 3];
long long f[mxM + 3];
void dfs(int pos, int cnt, long long x) {
if (pos == m) {
f[cnt + getbitcnt(x)]++;
return;
}
dfs(pos + 1, cnt, x);
dfs(pos + 1, cnt + 1, x ^ sta[pos]);
}
void solve() {
for (int i = sz; i < m; i++) {
for (int j = 0; j < sz; j++) {
if (a[b[j]] & (1ll << b[i])) {
sta[i] |= (1ll << j);
}
}
}
dfs(sz, 0, 0ll);
for (int k = 0; k <= m; k++) {
for (int i = 0; i <= m; i++) {
long long coe = 0ll;
for (int j = 0; j <= i && j <= k; j++) {
long long tmp = comb[i][j] * comb[m - i][k - j] % P;
if (j & 1) {
tmp = P - tmp;
}
coe += tmp - P, coe += (coe >> 31) & P;
}
coe = coe * f[i] % P;
ans[k] += coe - P, ans[k] += (ans[k] >> 31) & P;
}
ans[k] = ans[k] * quickpow((P + 1ll) / 2ll, m - sz) % P;
}
}
} // namespace Solve2
int main() {
initfact(mxM);
for (int i = 1; i < mxN; i++) bitcnt[i] = bitcnt[i >> 1] + (i & 1);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
long long x;
scanf("%I64d", &x);
for (int k = m - 1; k >= 0; k--)
if (x & (1ll << k)) {
if (!a[k]) {
a[k] = x;
sz++;
break;
} else {
x ^= a[k];
}
}
}
for (int i = m - 1; i >= 0; i--)
if (a[i]) {
for (int j = i + 1; j < m; j++)
if (a[j] & (1ll << i)) {
a[j] ^= a[i];
}
}
for (int i = 0, j = 0; i < sz; i++) {
while (!a[j]) {
j++;
}
b[i] = j;
j++;
}
for (int i = sz, j = 0; i < m; i++) {
while (a[j]) {
j++;
}
b[i] = j;
j++;
}
if (sz <= 26) {
Solve1::dfs(0, 0ll);
} else {
Solve2::solve();
}
for (int i = 0; i <= m; i++) ans[i] = ans[i] * quickpow(2ll, n - sz) % P;
for (int i = 0; i <= m; i++) printf("%I64d ", ans[i]);
puts("");
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
template <int MOD>
struct Integral {
int v_ = 0;
template <typename T>
Integral(T v) : v_(norm(v)) {
static_assert(std::is_integral<T>::value, "input should be an integral.");
}
Integral() = default;
~Integral() = default;
template <typename T>
T norm(T v) const {
if constexpr (std::is_same<long long, T>::value) {
v %= MOD;
if (v < 0) v += MOD;
} else {
if (v >= MOD) v -= MOD;
if (v < 0) v += MOD;
if (v >= MOD || v < 0) {
v %= MOD;
if (v < 0) v += MOD;
}
}
return v;
}
int val() const { return v_; }
Integral& operator+=(const Integral& rhs) {
v_ += rhs.val();
if (v_ >= MOD) v_ -= MOD;
return *this;
}
Integral& operator-=(const Integral& rhs) {
v_ += MOD - rhs.val();
if (v_ >= MOD) v_ -= MOD;
return *this;
}
Integral& operator*=(const Integral& rhs) {
v_ = v_ * 1LL * rhs.val() % MOD;
return *this;
}
Integral& operator/=(const Integral& rhs) {
v_ = v_ * 1LL * rhs.inv().val() % MOD;
return *this;
}
Integral operator+(const Integral& rhs) const {
Integral ret = *this;
return ret += rhs;
}
Integral operator-(const Integral& rhs) const {
Integral ret = *this;
return ret -= rhs;
}
Integral operator*(const Integral& rhs) const {
Integral ret = *this;
return ret *= rhs;
}
Integral operator/(const Integral& rhs) const {
Integral ret = *this;
return ret /= rhs;
}
bool operator==(const Integral& rhs) const { return val() == rhs.val(); }
bool operator!=(const Integral& rhs) const { return !(*this == rhs); }
const Integral operator-() const { return Integral(-val()); }
const Integral& operator++() {
v_ += 1;
if (v_ >= MOD) v_ -= MOD;
return *this;
}
const Integral operator++(int) {
Integral ret = *this;
++(*this);
return ret;
}
const Integral& operator--() {
v_ += MOD - 1;
if (v_ >= MOD) v_ -= MOD;
return *this;
}
const Integral operator--(int) {
Integral ret = *this;
--(*this);
return ret;
}
Integral power(long long b) const {
long long ret = 1 % MOD, a = v_;
for (; b; b >>= 1, a = a * a % MOD)
if (b & 1) ret = ret * a % MOD;
return ret;
}
Integral inv() const { return power(MOD - 2); }
};
template <int MOD>
std::string to_string(const Integral<MOD>& v) {
return std::string("{") + std::to_string(v.val()) + "}";
}
template <int MOD, bool kAllowBruteForce = false>
struct Binomial {
std::vector<Integral<MOD>> factor, inv_factor;
explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) {
factor[0] = 1;
for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i;
inv_factor[n] = factor[n].inv();
for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i;
}
~Binomial() = default;
template <typename T>
Integral<MOD> operator()(T a, T b) const {
if (a < b || b < 0) return 0;
if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b];
if constexpr (!kAllowBruteForce) {
throw std::out_of_range("Binomial");
} else {
b = std::min(b, a - b);
Integral<MOD> ret = 1;
for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i;
return ret;
}
}
};
template <int MOD>
struct PowerTable : public std::vector<Integral<MOD>> {
PowerTable(int n, const Integral<MOD>& g) {
static_assert(sizeof(PowerTable) == sizeof(std::vector<Integral<MOD>>), "");
this->resize(n + 1);
this->at(0) = 1;
this->at(1) = g;
for (int i = 2; i < this->size(); ++i)
this->at(i) = this->at(i - 1) * this->at(1);
}
};
const int MOD = 998244353;
using Mint = Integral<MOD>;
using Binom = Binomial<MOD>;
Binom binom(200000);
PowerTable<MOD> pw2(200000, 2);
PowerTable<MOD> pw_inv2(200000, Mint(2).inv());
using LL = long long;
const int kDim = 53 + 5;
Mint f[kDim];
Mint B[kDim];
std::vector<LL> a;
int n, m, k;
std::vector<int> major_dims, minor_dims;
std::vector<LL> influences;
void search_base(int at, LL mask) {
if (at == a.size()) {
++f[__builtin_popcountll(mask)];
return;
}
search_base(at + 1, mask);
search_base(at + 1, mask ^ a[at]);
}
void search_minor_dim(int at, LL mask, LL influence_mask) {
if (at == minor_dims.size()) {
int r = __builtin_popcountll(mask) + __builtin_popcountll(influence_mask);
++B[r];
return;
}
search_minor_dim(at + 1, mask, influence_mask);
search_minor_dim(at + 1, mask | 1LL << minor_dims[at],
influence_mask ^ influences[at]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::istream& reader = std::cin;
reader >> n >> m;
a.resize(n);
for (int i = 0; i < n; ++i) {
reader >> a[i];
}
k = 0;
for (int dim = 0; dim < m; ++dim) {
for (int i = k; i < n; ++i) {
if (a[i] >> dim & 1) {
std::swap(a[k], a[i]);
break;
}
}
if (k >= n || (~a[k] >> dim & 1)) {
minor_dims.emplace_back(dim);
continue;
}
for (int i = 0; i < n; ++i)
if (i != k && (a[i] >> dim & 1)) a[i] ^= a[k];
major_dims.emplace_back(dim);
++k;
}
a.resize(k);
if (k <= m - k) {
search_base(0, 0LL);
} else {
influences.resize(minor_dims.size());
for (int i = 0; i < minor_dims.size(); ++i) {
for (int j = 0; j < k; ++j)
if (a[j] >> minor_dims[i] & 1) influences[i] |= 1LL << j;
}
search_minor_dim(0, 0LL, 0LL);
for (int c = 0; c <= m; ++c) {
for (int r = 0; r <= m; ++r) {
Mint h = 0;
for (int j = 0; j <= std::min(r, c); ++j) {
Mint val = binom(r, j) * binom(m - r, c - j);
if (j & 1)
h -= val;
else
h += val;
}
f[c] += pw2[k] * pw_inv2[m] * h * B[r];
}
}
}
for (int c = 0; c <= m; ++c) {
Mint result = f[c] * pw2[n - k];
printf("%d%c", result.val(), " \n"[c == m]);
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long a[200010], b[100], v[100], p[100], r[100], t[100], pw[200010],
c[100][100], g[100][100], n, rk, m, ct;
void ins(long long x) {
for (long long i = m; ~i; i--)
if (x & (1ll << i)) {
if (b[i])
x ^= b[i];
else {
b[i] = x;
rk++;
return;
}
}
}
void dfs(long long x, long long y) {
if (x == ct) {
t[__builtin_popcountll(y)]++;
return;
}
dfs(x + 1, y);
dfs(x + 1, y ^ p[x]);
}
signed main() {
cin >> n >> m;
pw[0] = 1;
m--;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
ins(a[i]);
pw[i] = pw[i - 1] * 2 % 998244353;
}
for (long long i = 0; i <= m; i++)
for (long long j = 0; j < i; j++)
if (b[i] & (1ll << j)) b[i] ^= b[j];
if (rk <= 26) {
for (long long i = 0; i <= m; i++)
if (b[i]) p[ct++] = b[i];
dfs(0, 0);
for (long long i = 0; i <= m + 1; i++)
cout << pw[n - rk] * t[i] % 998244353 << ' ';
return 0;
}
for (long long i = 0; i <= m; i++)
for (long long j = 0; j <= m; j++) v[j] |= (b[i] >> j & 1) << i;
for (long long i = 0; i <= m; i++) {
v[i] ^= (1ll << i);
if (v[i]) p[ct++] = v[i];
}
m++;
rk = m - rk;
dfs(0, 0);
for (long long i = 0; i < 100; i++) c[i][0] = 1;
for (long long i = 1; i < 100; i++)
for (long long j = 1; j < 100; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 998244353;
for (long long i = 0; i <= m; i++)
for (long long j = 0; j <= m; j++)
for (long long k = 0; k <= i; k++) {
if (k & 1)
g[i][j] =
(g[i][j] - c[m - j][i - k] * c[j][k] % 998244353 + 998244353) %
998244353;
else
g[i][j] = (g[i][j] + c[m - j][i - k] * c[j][k]) % 998244353;
}
for (long long i = 0; i <= m; i++)
for (long long j = 0; j <= m; j++)
r[i] = (r[i] + t[j] * g[i][j]) % 998244353;
long long rr = pw[max(0ll, n - m + rk)];
for (long long i = 1; i <= rk; i++)
rr = rr * ((998244353 + 1) / 2) % 998244353;
for (long long i = 0; i <= m; i++) cout << r[i] * rr % 998244353 << ' ';
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T, class T2>
long long chkmin(T &a, T2 b) {
return a > b ? a = b, 1 : 0;
}
template <class T, class T2>
long long chkmax(T &a, T2 b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
return a * a;
}
template <class T, class T2>
T mmin(T a, T2 b) {
return a < b ? a : b;
}
template <class T, class T2>
T mmax(T a, T2 b) {
return a > b ? a : b;
}
template <class T>
T aabs(T a) {
return a < 0 ? -a : a;
}
template <class T>
long long dcmp(T a, T b) {
return a > b;
}
template <long long *a>
long long cmp_a(long long first, long long second) {
return a[first] < a[second];
}
struct __INIT__ {
__INIT__() { memset(&inf, 0x3f, sizeof(inf)); }
} __INIT___;
namespace io {
const long long SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
long long f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char first) {
*oS++ = first;
if (oS == oT) flush();
}
template <typename A>
inline bool read(A &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(char &first) {
while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == ' ' ||
first == '\n' || first == '\r')
;
return first != EOF;
}
inline bool read(char *first) {
while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == '\n' ||
*first == ' ' || *first == '\r')
;
if (*first == EOF) return 0;
while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
*(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
*first = 0;
return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
return read(first) && read(second...);
}
template <typename A>
inline bool write(A first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(char first) {
putc(first);
return 0;
}
inline bool write(const char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
inline bool write(char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
return write(first) || write(second...);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
long long a[55], b[55], l[55];
const long long p = 998244353;
long long n, m, first, k, pw = 0;
void insert(long long first) {
for (long long i = m - 1; ~i; --i)
if (first & (1ll << i)) {
if (!l[i]) {
l[i] = first;
for (long long j = 0; j < i; ++j)
if (l[i] & (1ll << j)) l[i] ^= l[j];
for (long long j = i + 1; j < m; ++j)
if (l[j] & (1ll << i)) l[j] ^= l[i];
return;
} else {
first ^= l[i];
}
}
++pw;
}
long long f1[1 << 20 | 5], f2[1 << 20 | 5];
long long cnt[55];
void dp(long long *a, long long k, long long *cnt) {
long long m = mmin(k, 14), m2 = k - m;
for (long long i = 0; i < m; ++i) f1[1 << i] = a[i + 1];
for (long long i = 0; i < m2; ++i) f2[1 << i] = a[i + m + 1];
for (long long i = 1; i < 1 << m; ++i)
if (i & (i - 1)) f1[i] = f1[i & (i - 1)] ^ f1[i & (-i)];
for (long long i = 1; i < 1 << m2; ++i)
if (i & (i - 1)) f2[i] = f2[i & (i - 1)] ^ f2[i & (-i)];
for (long long i = 0; i < 1 << m; ++i)
for (long long j = 0; j < 1 << m2; ++j)
++cnt[__builtin_popcount(i) + __builtin_popcount(j) +
__builtin_popcountll(f1[i] ^ f2[j])];
}
long long xc[55];
long long w[55][55];
long long c[55];
long long C[55][55];
void make(long long *a, long long *b, long long k) {
for (long long i = 0; i < k; ++i) a[i] = a[i + 1] << k | 1ll << i;
for (long long i = 0; i < m; ++i) c[i] = 1ll << i;
for (long long i = 0; i < k; ++i) {
for (long long j = i + 1; j < m; ++j)
if (__builtin_parityll(a[i] & c[j])) c[j] ^= c[i];
}
for (long long i = 1; i <= (m - k); ++i)
b[i] = c[i + k - 1] & ((1LL << k) - 1);
}
signed main() {
read(n, m);
for (long long i = 1; i <= n; ++i) {
read(first);
insert(first);
}
for (long long i = 0; i < m; ++i)
if (l[i]) {
++k;
long long L = 0;
for (long long j = 0; j < i; ++j)
if (!l[j]) {
if (l[i] & (1ll << j)) a[k] |= 1LL << L;
++L;
}
}
if (k <= 27) {
dp(a, k, cnt);
long long s = 1;
for (; pw; --pw) s = (s << 1) % p;
for (long long i = 0; i <= m; ++i)
write(cnt[i] * s % p, i == m ? '\n' : ' ');
} else {
make(a, b, k);
dp(b, m - k, xc);
pw += k;
pw -= m;
for (long long i = 0; i <= m; ++i) {
C[i][0] = C[i][i] = 1;
for (long long j = 1; j < i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % p;
}
for (long long i = 0; i <= m; ++i) {
for (long long j = 0; j <= m; ++j) {
for (long long k = 0; k <= i && k <= j; k += 2) {
w[i][j] = (w[i][j] + C[i][k] * C[m - i][j - k]) % p;
}
for (long long k = 1; k <= i && k <= j; k += 2) {
w[i][j] = (w[i][j] - C[i][k] * C[m - i][j - k]) % p;
}
if (w[i][j] < 0) w[i][j] += p;
}
}
long long s = 1;
if (pw < 0)
for (; pw < 0; ++pw) s = (s * (p + 1) >> 1) % p;
else
for (; pw; --pw) s = (s << 1) % p;
for (long long i = 0; i <= m; ++i)
for (long long j = 0; j <= m; ++j)
cnt[j] = (cnt[j] + xc[i] * w[i][j]) % p;
for (long long i = 0; i <= m; ++i)
write(cnt[i] * s % p, i == m ? '\n' : ' ');
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 2e5 + 5;
const int M = 110;
const int inv2 = (mod + 1) >> 1;
int n, m;
int bin[M][M];
struct LinarBase {
long long b[M];
int rk;
bool side[M];
void insert(long long s) {
for (int i = m - 1; ~i; i--)
if (1 & (s >> i)) {
if (b[i])
s ^= b[i];
else {
rk++, side[i] = 1, b[i] = s;
break;
}
}
}
void clr() {
for (int i = 0; i < m; i++)
for (int j = i + 1; j < m; j++) {
if (1 & (b[j] >> i)) b[j] ^= b[i];
}
}
} B;
vector<long long> Line;
int sz;
int p[M];
inline void dfs(int c, long long cur) {
if (c == sz) {
p[__builtin_popcountll(cur)]++;
return;
}
dfs(c + 1, cur ^ Line[c]);
dfs(c + 1, cur);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
B.insert(x);
}
B.clr();
if (B.rk <= m / 2) {
for (int i = 0; i < m; i++)
if (B.side[i]) Line.push_back(B.b[i]);
sz = Line.size();
dfs(0, 0);
int ans = 1;
for (int i = 1; i <= n - B.rk; i++) ans = ans * 2 % mod;
for (int i = 0; i <= m; i++) {
printf("%lld ", 1ll * ans * p[i] % mod);
}
} else {
for (int i = 0; i < m; i++)
if (!B.side[i]) {
Line.push_back(1ll << i);
for (int k = 0; k < m; k++)
if (B.side[k]) {
if (1 & (B.b[k] >> i)) Line.back() |= 1ll << k;
}
}
sz = Line.size();
dfs(0, 0);
bin[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = bin[i][0] = 1; j <= i; j++)
bin[i][j] = (bin[i - 1][j - 1] + bin[i - 1][j]) % mod;
}
for (int i = 0; i <= m; i++) {
int ans = 0;
for (int j = 0; j <= m; j++) {
int coef = 0;
for (int k = 0; k <= i; k++) {
coef = 1ll *
(coef +
(k & 1 ? -1ll : 1ll) * bin[j][k] * bin[m - j][i - k] % mod +
mod) %
mod;
}
ans = (ans + 1ll * coef * p[j] % mod) % mod;
}
for (int j = 0; j < m - B.rk; j++) {
ans = 1ll * ans * inv2 % mod;
}
for (int j = 0; j < n - B.rk; j++) {
ans = ans * 2 % mod;
}
printf("%d ", ans);
}
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
void generate_all() {
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
int64_t value = 0;
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
;
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
if (B > 32) {
vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
} else {
vector<vector<unsigned>> dp(count_sum + 1, vector<unsigned>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.6 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;
void _update_input_buffer() {
input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
input_pos = 0;
if (input_len == 0) input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
if (input_pos >= input_len) _update_input_buffer();
return input_buffer[advance ? input_pos++ : input_pos];
}
template <typename T>
inline void read_int(T &number) {
bool negative = false;
number = 0;
while (!isdigit(next_char(false)))
if (next_char() == '-') negative = true;
do {
number = 10 * number + (next_char() - '0');
} while (isdigit(next_char(false)));
if (negative) number = -number;
}
template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
read_int(number);
read_int(args...);
}
} // namespace IO
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
void row_reduce(vector<int64_t> &basis) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
}
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> counts(M + 1, 0);
int64_t value = 0;
if (B < 8) {
for (int mask = 0; mask < 1 << B; mask++) {
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
} else {
static const int JUMP = 32;
for (int mask = 0; mask < 1 << B; mask += JUMP) {
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[2];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[3];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[2];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[4];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[2];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[3];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[2];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[1];
counts[__builtin_popcountll(value)]++;
value ^= change[0];
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + JUMP)];
}
}
return counts;
}
vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int> highest_bit(B, -1);
vector<bool> bit_covered(M, false);
for (int i = 0; i < B; i++) {
highest_bit[i] = 63 - __builtin_clzll(basis[i]);
bit_covered[highest_bit[i]] = true;
}
vector<int64_t> orthogonal;
for (int bit = M - 1; bit >= 0; bit--)
if (!bit_covered[bit]) {
int64_t value = 1LL << bit;
for (int i = 0; i < B; i++)
value |= (basis[i] >> bit & 1) << highest_bit[i];
orthogonal.push_back(value);
}
return orthogonal;
}
vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> orthogonal = build_orthogonal(basis, M);
vector<int64_t> orthogonal_counts = generate_all(orthogonal, M);
vector<int64_t> counts(M + 1, 0);
vector<vector<int64_t>> choose(M + 1);
for (int n = 0; n <= M; n++) {
choose[n].resize(n + 1);
choose[n][0] = choose[n][n] = 1;
for (int r = 1; r < n; r++)
choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
}
auto &&get_choose = [&](int n, int r) -> int64_t {
if (r < 0 || r > n) return 0;
return choose[n][r];
};
for (int c = 0; c <= M; c++) {
vector<int64_t> w(M + 1, 0);
for (int d = 0; d <= M; d++)
for (int j = 0; j <= d; j++)
w[d] +=
(j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j);
for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d];
assert((counts[c] & ((1LL << (M - B)) - 1)) == 0);
counts[c] >>= M - B;
}
return counts;
}
int main() {
int N, M;
IO::read_int(N, M);
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
IO::read_int(a);
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(basis_obj.basis, basis_obj.basis + B);
vector<int64_t> answers =
B <= M / 2 ? generate_all(basis, M) : generate_from_orthogonal(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return x;
}
const int Mod = 998244353, inv2 = Mod + 1 >> 1, MAXW = (1 << 18) - 1;
int upd(int x) { return x + (x >> 31 & Mod); }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = 1LL * x * x % Mod)
if (y & 1) res = 1LL * res * x % Mod;
return res;
}
int n, m;
long long w[200010], lb[60], Atot;
long long bin[60];
int bintop;
int p[60], q[60], ans[60];
int ins(long long x) {
for (int i = m - 1; i >= 0; i--)
if (x >> i & 1) {
if (lb[i]) {
x ^= lb[i];
continue;
}
lb[i] = x;
for (int j = i - 1; j >= 0; j--)
if (lb[i] >> j & 1) lb[i] ^= lb[j];
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) lb[j] ^= lb[i];
return 1;
}
return 0;
}
void dfs(int *p) {
for (int i = 0; i < bintop; i++) bin[i] = bin[i + 1];
long long msk = 0;
p[0]++;
for (int i = 1; i < (1 << bintop); i++) {
msk ^= bin[__builtin_ctz(i)];
p[__builtin_popcountll(msk)]++;
}
}
int dp[60][60];
int C[60][60];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) Atot += ins(w[i] = read());
if (Atot <= 26) {
for (int i = 0; i < m; i++)
if (lb[i]) bin[++bintop] = lb[i];
dfs(p);
int t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
for (int i = 0; i < m; i++)
if (!lb[i]) {
long long x = 1LL << i;
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) x |= 1LL << j;
bin[++bintop] = x;
}
dfs(q);
for (int i = 0; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
for (int k = 0, t = 1; k <= i && k <= j; k++, t = Mod - t) {
dp[i][j] = (dp[i][j] + 1LL * t * C[j][k] % Mod * C[m - j][i - k]) % Mod;
}
}
for (int c = 0; c <= m; c++)
for (int i = 0; i <= m; i++) p[c] = (p[c] + 1LL * q[i] * dp[c][i]) % Mod;
int t = qpow(inv2, m - Atot);
for (int i = 0; i <= m; i++) p[i] = 1LL * p[i] * t % Mod;
t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 60;
const int mod = 998244353;
long long gi() {
long long x = 0, o = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, m, rk, ans[N], C[N][N], w[N][N], q[N];
long long a[N], b[N];
bool insert(long long v) {
for (int i = m - 1; ~i; i--)
if (v >> i & 1) {
if (!a[i]) {
a[i] = v;
return 1;
}
v ^= a[i];
}
return 0;
}
void dfs(int x, long long v, long long *a, int *ans) {
if (x == m) {
++ans[__builtin_popcountll(v)];
return;
}
if (!a[x])
dfs(x + 1, v, a, ans);
else
dfs(x + 1, v, a, ans), dfs(x + 1, v ^ a[x], a, ans);
}
void getb() {
int now = m - 1;
static int to[N];
static long long new_a[N];
for (int i = m - 1; ~i; i--)
if (a[i]) to[i] = now--;
for (int i = m - 1; ~i; i--)
if (!a[i]) to[i] = now--;
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) new_a[i] |= (a[i] >> j & 1) << (to[j]);
swap(a, new_a);
now = m - 1;
for (int i = m - 1; ~i; i--)
if (a[i]) to[i] = now--;
for (int i = m - 1; ~i; i--)
if (!a[i]) to[i] = now--;
for (int i = 0; i < m; i++) new_a[to[i]] = a[i];
swap(a, new_a);
for (int i = m - rk - 1; ~i; i--) b[i] |= 1ll << i;
for (int i = m - rk; i < m; i++)
for (int j = 0; j < m - rk; j++) b[j] |= (a[i] >> j & 1) << i;
}
int main() {
cin >> n >> m;
int coef = 1;
for (int i = 1; i <= n; i++) {
if (!insert(gi()))
coef = 2ll * coef % mod;
else
++rk;
}
if (rk <= m / 2) {
dfs(0, 0, a, ans);
} else {
for (int i = 0; i < N; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
for (int t = 0; t <= m; t++)
for (int c = 0; c <= m; c++)
for (int k = 0; k <= t && k <= c; k++)
w[t][c] = (w[t][c] + 1ll * ((k & 1) ? mod - 1 : 1) * C[t][k] % mod *
C[m - t][c - k]) %
mod;
for (int i = m - 1; ~i; i--)
if (a[i])
for (int j = i + 1; j < m; j++)
if (a[j] >> i & 1) a[j] ^= a[i];
getb();
dfs(0, 0, b, q);
int ibin = 1;
for (int i = 1; i <= m - rk; i++)
ibin = 1ll * ibin * ((mod + 1) >> 1) % mod;
for (int c = 0; c <= m; c++) {
for (int i = 0; i <= m; i++)
ans[c] = (ans[c] + 1ll * q[i] * w[i][c]) % mod;
ans[c] = 1ll * ans[c] * ibin % mod;
}
}
for (int i = 0; i <= m; i++) cout << 1ll * ans[i] * coef % mod << ' ';
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
struct MI {
private:
char bb[1 << 14];
FILE* f;
char *bs, *be;
char e;
bool o, l;
public:
MI() : f(stdin), bs(0), be(0) {}
inline char get() {
if (o) {
o = 0;
return e;
}
if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f);
if (bs == be) {
l = 1;
return -1;
};
return *bs++;
}
inline void unget(char c) {
o = 1;
e = c;
}
template <class T>
inline T read() {
T r;
*this > r;
return r;
}
template <class T>
inline MI& operator>(T&);
};
template <class T>
struct Q {
const static bool U = T(-1) >= T(0);
inline void operator()(MI& t, T& r) const {
r = 0;
char c;
bool y = 0;
if (U)
for (;;) {
c = t.get();
if (c == -1) goto E;
if (isdigit(c)) break;
}
else
for (;;) {
c = t.get();
if (c == -1) goto E;
if (c == '-') {
c = t.get();
if (isdigit(c)) {
y = 1;
break;
};
} else if (isdigit(c))
break;
;
};
for (;;) {
if (c == -1) goto E;
if (isdigit(c))
r = r * 10 + (c ^ 48);
else
break;
c = t.get();
}
t.unget(c);
E:;
if (y) r = -r;
}
};
template <>
struct Q<char> {};
template <class T>
inline MI& MI::operator>(T& t) {
Q<T>()(*this, t);
return *this;
}
template <class T>
std::ostream& operator<(std::ostream& out, const T& t) {
return out << t;
}
using std::cout;
MI cin;
const int $n = 200005;
const int $l = 55;
const int mod = 998244353;
inline int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline void Add(int& x, int y) {
if ((x += y) >= mod) x -= mod;
}
inline int sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; }
inline int n1(int v, int p) { return (p & 1) ? sub(0, v) : v; }
inline long long ksm(long long x, int p = mod - 2) {
long long ret = 1;
for (; p; p >>= 1, (x *= x) %= mod)
if (p & 1) (ret *= x) %= mod;
return ret;
}
int n, m, k, C[$l][$l], w[$l][$l], t[$l];
long long f[$l], A[$l], B[$l];
template <long long v[$l]>
void dfs(int x, long long p) {
if (x == k) {
++t[__builtin_popcountll(p)];
return;
}
dfs<v>(x + 1, p);
dfs<v>(x + 1, p ^ v[x]);
}
int main() {
cin > n > m;
for (auto __r = (m), i = (0); i <= __r; ++i) {
C[i][0] = 1;
for (auto __r = (i), j = ((decltype(i))1); j <= __r; ++j)
C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
}
for (auto __r = (n), i = ((decltype(n))1); i <= __r; ++i) {
long long x = cin.read<long long>();
for (int j = m; j--;) {
if (!((x >> j) & 1)) continue;
if (!f[j]) {
f[j] = x;
break;
}
x ^= f[j];
}
}
for (auto __r = (m - 1), i = ((decltype(m))0); i <= __r; ++i) {
if (!f[i]) continue;
for (auto __r = (i - 1), j = ((decltype(i))0); j <= __r; ++j)
if ((f[i] >> j) & 1) f[i] ^= f[j];
for (auto __r = (m - 1), j = ((decltype(m))0); j <= __r; ++j)
if (!f[j]) A[k] = (A[k] << 1) | ((f[i] >> j) & 1);
A[k] |= 1LL << (m - k - 1);
++k;
}
if (k <= 26) {
dfs<A>(0, 0);
const auto s = ksm(2, n - k);
for (auto __r = (m), i = (0); i <= __r; ++i)
cout < ((long long)t[i] * s % mod) < " \n"[i == m];
return 0;
}
k = m - k;
for (auto __r = (k - 1), i = ((decltype(k))0); i <= __r; ++i) {
for (auto __r = (m - k - 1), j = ((decltype(m - k))0); j <= __r; ++j)
if ((A[j] >> i) & 1) B[i] |= 1LL << j;
B[i] |= 1LL << (m - i - 1);
}
dfs<B>(0, 0);
const auto s = ksm(2, (n - m + mod - 1) % (mod - 1));
for (auto __r = (m), i = (0); i <= __r; ++i) {
int ans = 0;
for (auto __r = (m), j = (0); j <= __r; ++j) {
int c = 0;
for (auto __r = (i), t = (0); t <= __r; ++t)
Add(c, n1((long long)C[j][t] * C[m - j][i - t] % mod, t));
ans = (ans + (long long)t[j] * c) % mod;
}
cout < ((long long)ans * s % mod) < " \n"[i == m];
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int M = 55, P = 998244353;
int n, m, c[M][M], g[M][M];
long long b[M];
vector<long long> B, G;
int f[60], p[60];
long long pw(long long a, long long m) {
long long res = 1;
while (m) m & 1 ? res = res * a % P : 0, a = a * a % P, m >>= 1;
return res;
}
void insert(long long x) {
for (int i = 53; i >= int(0); --i)
if (x >> i & 1) {
if (!b[i]) return b[i] = x, void();
x ^= b[i];
}
assert(x == 0);
}
void add(int &x, long long y) { x = (x + y) % P; }
void add(int &x, int y) { x = (x + y) % P; }
void work() {
for (int i = 53; i >= int(0); --i)
if (b[i]) {
for (int j = i + 1; j <= int(53); ++j)
if (b[j] >> i & 1) b[j] ^= b[i];
}
for (int i = 0; i <= int(53); ++i)
if (b[i]) B.push_back(b[i]);
}
void dfs(int k, long long cur) {
if (k == 0) {
add(p[__builtin_popcountll(cur)], 1);
return;
}
dfs(k - 1, cur);
dfs(k - 1, cur ^ B[k - 1]);
}
void solve1() {
dfs(B.size(), 0);
long long coef = pw(2, n - B.size());
for (int x = 0; x <= int(m); ++x) p[x] = p[x] * coef % P;
for (int i = 0; i <= int(m); ++i) printf("%d%c", p[i], " \n"[i == m]);
}
void dfs2(int k, long long cur) {
if (k == 0) {
add(f[__builtin_popcountll(cur)], 1);
return;
}
dfs2(k - 1, cur);
dfs2(k - 1, cur ^ G[k - 1]);
}
void solve2() {
c[0][0] = 1;
for (int i = 1; i <= int(m); ++i) {
c[i][0] = 1;
for (int j = 1; j <= int(m); ++j)
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
}
auto sig = [](int x) { return 1 - 2 * (x & 1); };
for (int x = 0; x <= int(m); ++x)
for (int y = 0; y <= int(m); ++y) {
for (int z = 0; z <= int(min(x, y)); ++z) {
add(g[x][y], c[y][z] * 1ll * c[m - y][x - z] * sig(z));
}
add(g[x][y], P);
}
for (int i = 0; i <= int(m - 1); ++i)
if (b[i] == 0) {
long long s = 1ll << i;
for (int j = 0; j <= int(m - 1); ++j)
if (b[j] >> i & 1) s |= 1ll << j;
if (s) G.push_back(s);
}
dfs2(G.size(), 0);
long long coef = pw(pw(2, m - B.size()), P - 2);
for (int x = 0; x <= int(m); ++x) {
for (int y = 0; y <= int(m); ++y) {
add(p[x], f[y] * 1ll * g[x][y]);
}
p[x] = p[x] * coef % P;
}
coef = pw(2, n - B.size());
for (int x = 0; x <= int(m); ++x) p[x] = p[x] * coef % P;
for (int i = 0; i <= int(m); ++i) printf("%d%c", p[i], " \n"[i == m]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= int(n); ++i) {
long long x;
scanf("%lld", &x);
insert(x);
}
work();
if (B.size() <= 26)
solve1();
else
solve2();
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int const p = 998244353;
int mod(int x) { return x >= p ? x - p : x; }
int pw(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % p;
x = 1ll * x * x % p;
y >>= 1;
}
return res;
}
int n, m, k, ans[55], d[55][55], C[55][55];
long long b[55], c[55];
bool insert(long long x) {
for (int i = 0; i < m; i++)
if (x & (1ll << i)) {
if (!b[i]) {
b[i] = x;
return 1;
}
x ^= b[i];
}
return 0;
}
void dfs(int now, long long res) {
if (now == c[0] + 1) {
ans[__builtin_popcountll(res)]++;
return;
}
dfs(now + 1, res);
dfs(now + 1, res ^ c[now]);
}
void work1() {
for (int i = 0; i < m; i++)
if (b[i]) c[++c[0]] = b[i];
dfs(1, 0);
int t = pw(2, n - k);
for (int i = 0; i <= m; i++) printf("%lld ", 1ll * t * ans[i] % p);
}
void work2() {
for (int i = 1; i < m; i++)
for (int j = 0; j < i; j++)
if (b[j] & (1ll << i)) b[j] ^= b[i];
for (int i = 0; i < m; i++)
for (int j = 0; j <= m; j++)
if ((b[i] >> j) & 1) d[i][j] = d[j][i] = 1;
for (int i = 0; i < m; i++) d[i][i] = 1;
for (int i = 0; i < m; i++)
if (!b[i]) {
c[0]++;
for (int j = 0; j < m; j++)
if (d[i][j]) c[c[0]] |= 1ll << j;
}
dfs(1, 0);
C[0][0] = 1;
for (int i = 1; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) C[i][j] = mod(C[i - 1][j - 1] + C[i - 1][j]);
}
int t = pw(2, p - 1 + n - m);
for (int i = 0; i <= m; i++) {
int tmp = 0;
for (int j = 0; j <= m; j++) {
int tmp2 = 0;
for (int l = 0; l <= i; l++)
tmp2 = (tmp2 +
1ll * ((l & 1) ? p - 1 : 1) * C[j][l] % p * C[m - j][i - l]) %
p;
tmp = (tmp + 1ll * tmp2 * ans[j]) % p;
}
printf("%lld ", 1ll * t * tmp % p);
}
}
int main() {
scanf("%d%d", &n, &m);
long long x;
for (int i = 1; i <= n; i++) scanf("%lld", &x), k += insert(x);
if (k <= 26)
work1();
else
work2();
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, rk, r, c;
int C[65][65], h[65];
long long p[65], t[65], s[65];
inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int SUB(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline void insert(long long x) {
for (int i = m; i >= 0; i--)
if (x >> i & 1) {
if (p[i])
x ^= p[i];
else {
p[i] = x;
break;
}
}
}
inline void dfs1(int x, long long sum) {
if (x > c) {
h[__builtin_popcountll(sum)]++;
return;
}
dfs1(x + 1, sum);
dfs1(x + 1, sum ^ t[x]);
}
inline void dfs2(int x, long long sum, int t) {
if (x > r) {
h[__builtin_popcountll(sum) + t]++;
return;
}
dfs2(x + 1, sum, t);
dfs2(x + 1, sum ^ s[x], t + 1);
}
inline int mpow(int a, int x) {
x = (x + mod - 1) % (mod - 1);
int ans = 1;
while (x) {
if (x & 1) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
x >>= 1;
}
return ans;
}
int main() {
scanf("%d %d", &n, &m);
long long x;
for (int i = 1; i <= n; i++) scanf("%lld", &x), insert(x);
for (int i = 0; i <= m; i++)
for (int j = i + 1; j <= m; j++)
if (p[j] >> i & 1) p[j] ^= p[i];
for (int i = 0; i <= m; i++) rk += (p[i] > 0);
C[0][0] = 1;
for (int i = 1; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) C[i][j] = ADD(C[i - 1][j], C[i - 1][j - 1]);
}
for (int i = m; i >= 0; i--)
if (p[i]) t[++c] = p[i];
if (2 * rk <= m) {
dfs1(1, 0);
int tp = mpow(2, n - rk);
for (int i = 0; i <= m; i++) printf("%lld ", 1ll * tp * h[i] % mod);
puts("");
} else {
for (int i = m - 1; i >= 0; i--) {
if (p[i]) continue;
r++;
for (int j = 1; j <= c; j++)
if (t[j] >> i & 1) s[r] |= (1ll << j - 1);
}
dfs2(1, 0, 0);
int tp = mpow(2, n - m);
for (int i = 0; i <= m; i++) {
int ans = 0;
for (int t = 0; t <= m; t++)
for (int j = 0, fl = 0; j <= min(t, i); j++, fl ^= 1) {
int tmp = 1ll * C[t][j] * C[m - t][i - j] % mod * h[t] % mod;
ans = fl ? SUB(ans, tmp) : ADD(ans, tmp);
}
printf("%lld ", 1ll * ans * tp % mod);
}
puts("");
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void cmin(T &a, const T &b) {
((a > b) && (a = b));
}
template <class T>
inline void cmax(T &a, const T &b) {
((a < b) && (a = b));
}
char IO;
template <class T = int>
T rd() {
T s = 0;
int f = 0;
while (!isdigit(IO = getchar()))
if (IO == '-') f = 1;
do s = (s << 1) + (s << 3) + (IO ^ '0');
while (isdigit(IO = getchar()));
return f ? -s : s;
}
const int N = 2e5 + 10, K = 19, P = 998244353;
int n, m, k;
long long qpow(long long x, long long k = P - 2) {
long long res = 1;
for (; k; k >>= 1, x = x * x % P)
if (k & 1) res = res * x % P;
return res;
}
long long d[60], e[60], dp[60], w[60][60], C[60][60];
namespace Enum {
long long d[60], c;
long long s[1 << 14], t[1 << 14];
void Solve(long long *D) {
for (int i = 0, iend = m - 1; i <= iend; ++i)
if (D[i]) d[c++] = D[i];
int m = c / 2;
for (int i = 0, iend = (1 << m) - 1; i <= iend; ++i)
for (int j = 0, jend = m - 1; j <= jend; ++j)
if (i & (1 << j)) s[i] ^= d[j];
for (int i = 0, iend = (1 << (c - m)) - 1; i <= iend; ++i)
for (int j = m, jend = c - 1; j <= jend; ++j)
if (i & (1 << (j - m))) t[i] ^= d[j];
int A = (1 << m) - 1;
for (int i = 0, iend = (1 << c) - 1; i <= iend; ++i)
dp[__builtin_popcountll(s[i & A] ^ t[i >> m])]++;
}
} // namespace Enum
int key[60], nonkey[60], c;
int main() {
for (int i = 0, iend = 56; i <= iend; ++i)
for (int j = C[i][0] = 1, jend = i; j <= jend; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P;
n = rd(), m = rd();
for (int t = 1, tend = n; t <= tend; ++t) {
long long x = rd<long long>();
for (int i = m - 1, iend = 0; i >= iend; --i)
if (x & (1ll << i)) {
if (!d[i]) {
d[i] = x;
n--, k++;
break;
} else
x ^= d[i];
}
}
memset(key, -1, sizeof key);
for (int i = m - 1, iend = 0; i >= iend; --i)
if (d[i]) {
for (int j = i - 1, jend = 0; j >= jend; --j)
if (d[i] & (1ll << j)) d[i] ^= d[j];
}
c = k = 0;
for (int i = 0, iend = m - 1; i <= iend; ++i)
if (d[i]) {
k++;
e[m - k] |= 1ll << (m - k);
for (int j = 0, jend = i - 1; j <= jend; ++j)
if (d[i] & (1ll << j)) {
if (key[j] == -1) key[j] = c++;
e[m - k] |= 1ll << key[j];
}
}
memset(d, 0, sizeof d), swap(d, e);
n = qpow(2, n);
if (k <= 26) {
Enum::Solve(d);
for (int i = 0, iend = m; i <= iend; ++i) printf("%d ", dp[i] * n % P);
return 0;
}
for (int i = m - 1, iend = 0; i >= iend; --i) {
if (!d[i]) e[i] |= 1ll << i;
for (int j = i - 1, jend = 0; j >= jend; --j)
if (d[i] & (1ll << j)) e[j] |= 1ll << i;
}
Enum::Solve(e);
for (int i = 0, iend = m; i <= iend; ++i)
for (int j = 0, jend = m; j <= jend; ++j) {
for (int k = 0, kend = min(i, j); k <= kend; ++k)
w[i][j] = (w[i][j] + C[j][k] * C[m - j][i - k] * (k & 1 ? -1 : 1)) % P;
((w[i][j] < 0) && (w[i][j] += P));
}
n = n * qpow((P + 1) / 2, m - k) % P;
for (int i = 0, iend = m; i <= iend; ++i) {
long long ans = 0;
for (int j = 0, jend = m; j <= jend; ++j) ans = (ans + dp[j] * w[i][j]) % P;
printf("%lld ", ans * n % P);
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
namespace IO {
char gc() { return getchar(); }
template <typename Tp>
bool get1(Tp &x) {
bool neg = 0;
char c = gc();
while (c != EOF && (c < '0' || c > '9') && c != '-') c = gc();
if (c == '-') c = gc(), neg = 1;
if (c == EOF) return false;
x = 0;
for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + c - '0';
if (neg) x = -x;
return true;
}
template <typename Tp>
void printendl(Tp x) {
if (x < 0) putchar('-'), x = -x;
static short a[40], sz;
sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
puts("");
}
} // namespace IO
using IO::get1;
using IO::printendl;
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const int maxn = 100111;
const int mod = 998244353;
int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1ll * ret * x % mod;
x = 1ll * x * x % mod;
y >>= 1;
}
return ret;
}
inline void add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
int n, m, tot, coef;
long long a[111], b[111];
int cnt[111], ans[111];
void work() {
long long now = 0;
cnt[0] = 1;
for (int i = 1; i < (1 << tot); i++) {
now ^= a[__builtin_ctz(i)];
cnt[__builtin_popcountll(now)]++;
}
}
int main() {
get1(n) && get1(m);
coef = 1;
while (n--) {
long long x;
get1(x);
for (int i = 0; i < tot; i++)
if (a[i] & -a[i] & x) x ^= a[i];
if (!x) {
add(coef, coef);
continue;
}
for (int i = 0; i < tot; i++)
if (a[i] & x & -x) a[i] ^= x;
a[tot++] = x;
}
if (tot <= m / 2) {
work();
for (int i = 0; i <= m; i++) ans[i] = 1ll * cnt[i] * coef % mod;
} else {
static int seq[111], have[111], c[111][111], ntot;
for (int i = 0; i <= m; i++) {
c[i][0] = 1;
for (int j = 1; j <= m; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
for (int i = 0; i < tot; i++) have[__builtin_ctzll(a[i])] = 1;
for (int i = 0; i < m; i++)
if (!have[i]) {
seq[ntot++] = i;
if (coef & 1) coef += mod;
coef >>= 1;
}
for (int i = 0; i < ntot; i++) {
b[i] = 1ll << m - i;
for (int j = 0; j < tot; j++)
if ((a[j] >> seq[i]) & 1) b[i] |= 1ll << j;
}
tot = ntot;
memcpy(a, b, sizeof(a));
work();
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
int sum = 0;
for (int k = 0; k <= j; k++) {
int now = 1ll * c[j][k] * c[m - j][i - k] % mod;
if (k % 2 == 0)
add(sum, now);
else
add(sum, mod - now);
}
add(ans[i], 1ll * sum * cnt[j] % mod);
}
ans[i] = 1ll * ans[i] * coef % mod;
}
}
for (int i = 0; i <= m; i++) printf("%d%c", ans[i], i == m ? '\n' : ' ');
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
namespace IO {
char gc() { return getchar(); }
template <typename Tp>
bool get1(Tp &x) {
bool neg = 0;
char c = gc();
while (c != EOF && (c < '0' || c > '9') && c != '-') c = gc();
if (c == '-') c = gc(), neg = 1;
if (c == EOF) return false;
x = 0;
for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + c - '0';
if (neg) x = -x;
return true;
}
template <typename Tp>
void printendl(Tp x) {
if (x < 0) putchar('-'), x = -x;
static short a[40], sz;
sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
puts("");
}
} // namespace IO
using IO::get1;
using IO::printendl;
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const int maxn = 100111;
const int mod = 998244353;
int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1ll * ret * x % mod;
x = 1ll * x * x % mod;
y >>= 1;
}
return ret;
}
inline void add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
int n, m, tot, coef;
long long a[111], b[111];
int cnt[111], ans[111];
void dfs(int x, long long now) {
if (x == tot) {
cnt[__builtin_popcountll(now)]++;
return;
}
dfs(x + 1, now);
dfs(x + 1, now ^ a[x]);
}
int main() {
get1(n) && get1(m);
coef = 1;
while (n--) {
long long x;
get1(x);
for (int i = 0; i < tot; i++)
if (a[i] & -a[i] & x) x ^= a[i];
if (!x) {
add(coef, coef);
continue;
}
for (int i = 0; i < tot; i++)
if (a[i] & x & -x) a[i] ^= x;
a[tot++] = x;
}
if (tot <= m / 2) {
dfs(0, 0);
for (int i = 0; i <= m; i++) ans[i] = 1ll * cnt[i] * coef % mod;
} else {
static int seq[111], have[111], c[111][111], ntot;
for (int i = 0; i <= m; i++) {
c[i][0] = 1;
for (int j = 1; j <= m; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
for (int i = 0; i < tot; i++) have[__builtin_ctzll(a[i])] = 1;
for (int i = 0; i < m; i++)
if (!have[i]) {
seq[ntot++] = i;
if (coef & 1) coef += mod;
coef >>= 1;
}
for (int i = 0; i < ntot; i++) {
b[i] = 1ll << m - i;
for (int j = 0; j < tot; j++)
if ((a[j] >> seq[i]) & 1) b[i] |= 1ll << j;
}
tot = ntot;
memcpy(a, b, sizeof(a));
dfs(0, 0);
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
int sum = 0;
for (int k = 0; k <= j; k++) {
int now = 1ll * c[j][k] * c[m - j][i - k] % mod;
if (k % 2 == 0)
add(sum, now);
else
add(sum, mod - now);
}
add(ans[i], 1ll * sum * cnt[j] % mod);
}
ans[i] = 1ll * ans[i] * coef % mod;
}
}
for (int i = 0; i <= m; i++) printf("%d%c", ans[i], i == m ? '\n' : ' ');
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long N = 57;
const long long M = 262144;
const long long INF = 1e9 + 7;
const long long mod = 998244353;
long long Pow(long long x, long long y) {
long long ans = 1, now = x;
while (y) {
if (y & 1) ans = ans * now % mod;
now = now * now % mod;
y >>= 1;
}
return ans;
}
long long n, m, ss = 0, st = 0;
long long t[N], a[N], bc[M], u[N], b[N], g[N], p[N][N], f[N], h[N][N], fac[N],
inv[N];
long long Bc(long long x) { return bc[x / M / M] + bc[x / M % M] + bc[x % M]; }
long long C(long long x, long long y) {
if (x < y || y < 0) return 0;
return fac[x] * inv[y] % mod * inv[x - y] % mod;
}
void dfs1(long long x, long long v) {
if (x == ss + 1) {
u[Bc(v)]++;
return;
}
dfs1(x + 1, v);
dfs1(x + 1, v ^ t[a[x]]);
}
void dfs2(long long x, long long v, long long s) {
if (x == st + 1) {
p[s][Bc(v)]++;
return;
}
dfs2(x + 1, v, s);
dfs2(x + 1, v ^ g[x], s + 1);
}
int main() {
scanf("%lld%lld", &n, &m);
for (long long i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
for (long long j = m - 1; ~j; j--) {
if ((x >> j) & 1ll) {
if (!t[j]) {
t[j] = x;
break;
}
x ^= t[j];
}
}
}
for (long long i = 0; i <= M - 1; i++) {
long long nw = i;
bc[i] = 0;
while (nw) {
bc[i] += (nw & 1);
nw >>= 1;
}
}
for (long long i = 0; i <= m - 1; i++)
if (t[i])
a[++ss] = i;
else
b[++st] = i;
long long pw = 1;
for (long long i = 1; i <= n - ss; i++) pw = (pw + pw) % mod;
if (ss <= (m + 1) / 2) {
dfs1(1, 0);
for (long long i = 0; i <= m + 1 - 1; i++) printf("%lld ", u[i] * pw % mod);
} else {
fac[0] = 1;
for (long long i = 1; i <= st; i++) fac[i] = fac[i - 1] * i % mod;
inv[st] = Pow(fac[st], mod - 2);
for (long long i = st; i; i--) inv[i - 1] = inv[i] * i % mod;
for (long long i = 0; i <= st + 1 - 1; i++) {
for (long long j = 0; j <= st + 1 - 1; j++) {
h[i][j] = 0;
for (long long k = 0; k <= i + 1 - 1; k++) {
if (k & 1)
h[i][j] = (h[i][j] - C(i, k) * C(st - i, j - k) % mod + mod) % mod;
else
h[i][j] = (h[i][j] + C(i, k) * C(st - i, j - k)) % mod;
}
}
}
for (long long i = 0; i <= m - 1; i++)
if (t[i])
for (long long j = i + 1; j <= m - 1; j++)
if ((t[j] >> i) & 1) t[j] ^= t[i];
for (long long i = 1; i <= st; i++) {
g[i] = 0;
for (long long j = 1; j <= ss; j++)
g[i] = (g[i] << 1) | ((t[a[j]] >> b[i]) & 1);
}
dfs2(1, 0, 0);
for (long long j = 0; j <= ss + 1 - 1; j++) {
for (long long i = 0; i <= ss + 1 - 1; i++) f[i] = 0;
f[0] = 1;
for (long long i = 1; i <= ss - j; i++)
for (long long k = ss; k; k--) f[k] = (f[k] + f[k - 1]) % mod;
for (long long i = 1; i <= j; i++)
for (long long k = ss; k; k--) f[k] = (f[k] - f[k - 1] + mod) % mod;
for (long long i = 0; i <= ss + 1 - 1; i++)
for (long long k = 0; k <= st + 1 - 1; k++)
for (long long o = 0; o <= st + 1 - 1; o++)
u[i + o] = (u[i + o] + p[k][j] * h[k][o] % mod * f[i]) % mod;
}
long long iv = Pow(Pow(2, st), mod - 2);
for (long long i = 0; i <= m + 1 - 1; i++)
printf("%lld ", u[i] * iv % mod * pw % mod);
puts("");
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1, M = 64, mod = 998244353;
inline void check(int& x) { x -= mod, x += x >> 31 & mod; }
int n, k, m, tot;
long long int a[N], f[M], g[M], t[M];
int ans[M], tmp[M], pw[N], c[M][M], w[M][M];
inline void ins(long long int x) {
for (int i = m; i >= 0; i--)
if (x >> i & 1) {
if (f[i])
x ^= f[i];
else
return f[i] = x, k++, void();
}
}
inline void dfs(int p, long long int x) {
if (p == tot)
tmp[__builtin_popcountll(x)]++;
else
dfs(p + 1, x), dfs(p + 1, x ^ t[p]);
}
int main() {
cin.tie(0), cin >> n >> m, pw[0] = 1, m--;
for (int i = 1; i <= n; i++)
cin >> a[i], ins(a[i]), check(pw[i] = pw[i - 1] << 1);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= i - 1; j++)
if (f[i] >> j & 1) f[i] ^= f[j];
if (k <= 26) {
for (int i = 0; i <= m; i++)
if (f[i]) t[tot++] = f[i];
dfs(0, 0);
for (int i = 0; i <= m + 1; i++)
cout << 1ll * pw[n - k] * tmp[i] % mod << ' ';
} else {
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) g[j] |= (f[i] >> j & 1) << i;
for (int i = 0; i <= m; i++)
if (g[i] ^= 1ll << i) t[tot++] = g[i];
k = ++m - k, dfs(0, 0);
for (int i = 0; i <= m; i++) c[i][0] = 1;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
check(c[i][j] = c[i - 1][j] + c[i - 1][j - 1]);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
for (int p = 0; p <= i; p++)
if (p & 1)
check(w[i][j] += mod - 1ll * c[j][p] * c[m - j][i - p] % mod);
else
check(w[i][j] += 1ll * c[j][p] * c[m - j][i - p] % mod);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
check(ans[i] += 1ll * tmp[j] * w[i][j] % mod);
int tag = pw[max(0, n - m + k)];
for (int i = 1; i <= k; i++) tag = 1ll * (mod + 1 >> 1) * tag % mod;
for (int i = 0; i <= m; i++) cout << 1ll * ans[i] * tag % mod << ' ';
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Z2Basis {
long long int a[53];
int sz;
void add(long long int x) {
for (int i = 0; i < 53; i++)
if ((x >> i) & 1) {
if ((a[i] >> i) & 1)
x ^= a[i];
else {
a[i] = x;
sz++;
return;
}
}
}
void norm() {
for (int i = 0; i < 53; i++)
for (int j = 0; j < i; j++)
if ((a[i] >> i) & 1)
if ((a[j] >> i) & 1) a[j] ^= a[i];
}
} Z2;
const int mod = 998244353;
int cnt[54];
vector<long long int> todo;
void dfs(int idx, long long int cur) {
if (idx == todo.size()) {
cnt[__builtin_popcountll(cur)]++;
return;
}
dfs(idx + 1, cur);
dfs(idx + 1, cur ^ todo[idx]);
}
long long int C[54][54];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
C[0][0] = 1;
for (int i = 1; i <= 53; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long int x;
cin >> x;
Z2.add(x);
}
Z2.norm();
if (Z2.sz <= 26) {
for (int i = 0; i < 53; i++)
if (Z2.a[i] != 0) todo.emplace_back(Z2.a[i]);
dfs(0, 0);
long long int c = 1;
for (int i = Z2.sz; i < n; i++) c = c * 2 % mod;
for (int i = 0; i <= m; i++) cout << c * cnt[i] % mod << ' ';
cout << endl;
} else {
for (int i = 0; i < m; i++)
if (Z2.a[i] == 0) {
long long int cur = 1LL << i;
for (int j = 0; j < m; j++)
if ((Z2.a[j] >> i) & 1) cur ^= 1LL << j;
todo.emplace_back(cur);
}
dfs(0, 0);
long long int c = 1;
for (int i = 0; i < m - Z2.sz; i++) c = c * ((mod + 1) / 2) % mod;
for (int i = 0; i < n - Z2.sz; i++) c = c * 2 % mod;
for (int i = 0; i <= m; i++) {
long long int ans = 0;
for (int j = 0; j <= m; j++) {
long long int w = 0;
for (int k = 0; k <= min(i, j); k++) {
if (k & 1)
w = (w - C[j][k] * C[m - j][i - k]) % mod;
else
w = (w + C[j][k] * C[m - j][i - k]) % mod;
}
w = (w % mod + mod) % mod;
ans = (ans + w * cnt[j]) % mod;
}
cout << ans * c % mod << ' ';
}
cout << endl;
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long qpow(long long x, long long y) {
while (y < 0) y += 998244353 - 1;
long long res = 1;
while (y) {
if (y & 1) res = res * x % 998244353;
x = x * x % 998244353;
y = y / 2;
}
return res;
}
int n, m;
long long vis[200], ypa;
void ins(long long x) {
for (int i = 0; i < m; i++)
if ((1ll << i) & x) {
if (vis[i])
x ^= vis[i];
else {
vis[i] = x;
return;
}
}
}
vector<long long> vivi, kiwi;
long long cnt[200];
void dfs(int x, long long what) {
if (x == vivi.size()) {
cnt[__builtin_popcountll(what)]++;
return;
}
dfs(x + 1, what);
dfs(x + 1, what ^ vivi[x]);
}
int c[200][200];
long long ap[200];
long long flu[200];
void efs(int x, long long what, int cc) {
if (x == kiwi.size()) {
cnt[cc + __builtin_popcountll(what)]++;
return;
}
efs(x + 1, what, cc);
efs(x + 1, what ^ flu[x], cc + 1);
}
signed main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld", &ypa), ins(ypa);
for (int i = 0; i < m; i++)
if (vis[i]) {
for (int j = 0; j < m; j++)
if (vis[j] & (1ll << i))
if (i != j) vis[j] ^= vis[i];
}
for (int i = 0; i < m; i++)
if (vis[i])
vivi.push_back(vis[i]);
else
kiwi.push_back(i);
if (vivi.size() <= kiwi.size() and 1) {
dfs(0, 0);
for (int i = 0; i <= m; i++)
printf("%d ",
(cnt[i] * qpow(2, n - (int)vivi.size()) % 998244353 + 998244353) %
998244353);
} else {
c[0][0] = 1;
for (int i = 1; i < 200; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 998244353;
}
for (int i = 0; i < kiwi.size(); i++)
for (int j = 0; j < vivi.size(); j++)
if (vivi[j] & (1ll << kiwi[i])) flu[i] |= (1ll << j);
efs(0, 0, 0);
for (int k = 0; k <= m; k++) {
long long ans = 0;
for (int i = 0; i <= m; i++) {
ap[i] = 0;
for (int j = 0, t = 1; j <= i and j <= k; j++, t = -t)
ap[i] = (ap[i] + 1ll * t * c[i][j] * c[m - i][k - j]) % 998244353;
ans = (ans + cnt[i] * ap[i]) % 998244353;
}
printf("%d ",
(ans * qpow(2, -m + n) % 998244353 + 998244353) % 998244353);
}
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long qpow(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) r = r * a % 998244353;
a = a * a % 998244353, b >>= 1;
}
return r;
}
long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x * f;
}
long long n, m, b[200005], a[200005], c[200005], ans[200005], d[200005], cnt,
fac[200005], inv[200005], dp[60][60], w[200005];
void insert(long long x) {
for (long long i = (m); i >= (0); --i) {
if (!(x & (1ll << i))) continue;
if (d[i])
x ^= d[i];
else
return d[i] = x, ++cnt, void();
}
}
long long C(long long n, long long m) {
if (n < m) return 0;
return fac[n] * inv[m] % 998244353 * inv[n - m] % 998244353;
}
void dfs(long long x, long long S) {
if (x > m) return ++ans[__builtin_popcountll(S)], void();
dfs(x + 1, S);
if (d[x]) dfs(x + 1, S ^ d[x]);
}
void dfs1(long long x, long long S) {
if (x < 0) return ++w[__builtin_popcountll(S)], void();
dfs1(x - 1, S);
if (b[x]) dfs1(x - 1, S ^ b[x]);
}
signed main() {
n = read(), m = read(), inv[0] = fac[0] = 1;
for (long long i = (1); i <= (m); ++i)
fac[i] = fac[i - 1] * i % 998244353, inv[i] = qpow(fac[i], 998244353 - 2);
for (long long i = (1); i <= (n); ++i) a[i] = read(), insert(a[i]);
if (cnt <= 26)
dfs(0, 0);
else {
for (long long i = (m); i >= (0); --i)
for (long long j = (m); j >= (0); --j)
if ((d[j] & (1ll << i)) && (i != j)) d[j] ^= d[i];
for (long long i = (m - 1); i >= (0); --i) {
if (d[i]) continue;
b[i] = (1ll << i);
for (long long j = (0); j <= (m); ++j)
if (d[j] & (1ll << i)) b[i] |= (1ll << j);
}
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (m); ++j)
for (long long k = (0); k <= (m); ++k) {
if (k & 1)
dp[i][j] =
(dp[i][j] - C(j, k) * C(m - j, i - k) % 998244353 + 998244353) %
998244353;
else
dp[i][j] =
(dp[i][j] + C(j, k) * C(m - j, i - k) % 998244353) % 998244353;
}
dfs1(m, 0);
for (long long i = (0); i <= (m); ++i)
for (long long j = (0); j <= (m); ++j)
ans[j] = (ans[j] + 1ll * dp[j][i] * w[i] % 998244353) % 998244353;
for (long long i = (0); i <= (m); ++i)
ans[i] = qpow(2, cnt) * ans[i] % 998244353 *
qpow(qpow(2, m), 998244353 - 2) % 998244353;
}
for (long long i = (0); i <= (m); ++i)
printf("%lld ", ans[i] * qpow(2, n - cnt) % 998244353);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int mod_inv(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return mod_inv(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
const int POPCOUNT_BITS = 14;
const int POPCOUNT_MASK = (1 << POPCOUNT_BITS) - 1;
vector<int8_t> _popcount;
void build_popcount() {
_popcount.resize(1 << POPCOUNT_BITS);
for (int x = 0; x < 1 << POPCOUNT_BITS; x++)
_popcount[x] = _popcount[x >> 1] + (x & 1);
}
int popcountll(int64_t x) {
return _popcount[x & POPCOUNT_MASK] +
_popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] +
_popcount[x >> (2 * POPCOUNT_BITS) & POPCOUNT_MASK] +
_popcount[x >> (3 * POPCOUNT_BITS)];
}
int popcount(int x) {
return _popcount[x & POPCOUNT_MASK] +
_popcount[x >> POPCOUNT_BITS & POPCOUNT_MASK] +
_popcount[x >> (2 * POPCOUNT_BITS)];
}
void generate_all() {
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
int64_t value = 0;
for (int64_t mask = 0; mask < 1LL << B; mask++) {
answers[popcountll(value)]++;
value ^= change[__builtin_ctzll(mask + 1)];
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
;
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
if (R <= 20) {
vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + __builtin_popcount(mask)] += dp[count][mask];
} else {
vector<vector<unsigned>> dp(count_sum - 1, vector<unsigned>(1 << R, 0));
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = min(max_count - 1, count_sum - 1 - opt_count - 1);
count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
dp[opt_count - 1][opt_mask]++;
max_count += opt_count;
}
for (int count = 0; count < count_sum - 1; count++)
for (int mask = 0; mask < 1 << R; mask++)
if (dp[count][mask] > 0)
answers[count + 1 + __builtin_popcount(mask)] += dp[count][mask];
int everything = 0;
for (int i = 0; i < B; i++) everything ^= options[i].second;
answers[count_sum + __builtin_popcount(everything)]++;
answers[0]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
build_popcount();
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.57 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long pow_mod(long long x, int k) {
long long ans = 1;
while (k) {
if (k & 1) ans = ans * x % 998244353;
x = x * x % 998244353;
k >>= 1;
}
return ans;
}
long long C[60][60];
void pre(int n) {
for (int i = 0; i <= n; i++) C[i][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 998244353;
}
namespace LB {
long long num[60];
bool insert(long long x, int k) {
for (int i = k - 1; i >= 0; i--)
if ((x >> i) & 1) {
if (!num[i]) {
num[i] = x;
return 1;
} else
x ^= num[i];
}
return 0;
}
void gause(int k) {
for (int i = k - 1; i >= 0; i--)
if (num[i]) {
for (int j = i + 1; j < k; j++)
if ((num[j] >> i) & 1) num[j] ^= num[i];
}
}
} // namespace LB
int ans[60];
long long a[60], b[60];
int up;
void dfs1(int d, long long s) {
if (d > up) {
ans[__builtin_popcountll(s)]++;
return;
}
dfs1(d + 1, s);
dfs1(d + 1, s ^ a[d]);
}
int cnt[60];
void dfs2(int d, long long s) {
if (d > up) {
cnt[__builtin_popcountll(s)]++;
return;
}
dfs2(d + 1, s);
dfs2(d + 1, s ^ b[d]);
}
bool vis[60];
int main() {
int n, m;
scanf("%d%d", &n, &m);
pre(m);
for (int i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
LB::insert(x, m);
}
LB::gause(m);
int sz = 0;
for (int i = m - 1; i >= 0; i--)
if (LB::num[i]) {
a[++sz] = LB::num[i];
vis[i] = 1;
}
if (sz * 2 <= m) {
up = sz;
dfs1(1, 0);
} else {
for (int i = 0; i < m; i++)
if (!vis[i]) {
b[++up] = (1LL << i);
for (int j = i + 1; j < m; j++)
if (LB::num[j] && (((LB::num[j]) >> i) & 1)) b[up] ^= (1LL << j);
}
dfs2(1, 0);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= i; j++)
for (int k = 0; k <= m - i; k++)
ans[j + k] = (ans[j + k] + C[i][j] * C[m - i][k] % 998244353 *
((j & 1) ? 998244353 - 1 : 1) %
998244353 * cnt[i]) %
998244353;
long long inv = pow_mod(499122177, up);
for (int i = 0; i <= m; i++) ans[i] = ans[i] * inv % 998244353;
}
long long v = pow_mod(2, n - sz);
for (int i = 0; i <= m; i++) printf("%lld ", ans[i] * v % 998244353);
printf("\n");
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
void row_reduce(vector<int64_t> &basis) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
}
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> counts(M + 1, 0);
int64_t value = 0;
for (int mask = 0; mask < 1 << B; mask++) {
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
return counts;
}
vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int> highest_bit(B, -1);
vector<bool> bit_covered(M, false);
for (int i = 0; i < B; i++) {
highest_bit[i] = 63 - __builtin_clzll(basis[i]);
bit_covered[highest_bit[i]] = true;
}
vector<int64_t> orthogonal;
for (int bit = M - 1; bit >= 0; bit--)
if (!bit_covered[bit]) {
int64_t value = 1LL << bit;
for (int i = 0; i < B; i++)
value |= (basis[i] >> bit & 1) << highest_bit[i];
orthogonal.push_back(value);
}
return orthogonal;
}
vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> orthogonal = build_orthogonal(basis, M);
vector<int64_t> orthogonal_counts = generate_all(orthogonal, M);
vector<int64_t> counts(M + 1, 0);
vector<vector<int64_t>> choose(M + 1);
for (int n = 0; n <= M; n++) {
choose[n].resize(n + 1);
choose[n][0] = choose[n][n] = 1;
for (int r = 1; r < n; r++)
choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
}
auto &&get_choose = [&](int n, int r) -> int64_t {
if (r < 0 || r > n) return 0;
return choose[n][r];
};
for (int c = 0; c <= M; c++) {
vector<int64_t> w(M + 1, 0);
for (int d = 0; d <= M; d++)
for (int j = 0; j <= d; j++)
w[d] +=
(j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j);
for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d];
assert((counts[c] & ((1LL << (M - B)) - 1)) == 0);
counts[c] >>= M - B;
}
return counts;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
vector<int64_t> answers;
if (B <= M / 2)
answers = generate_all(basis, M);
else
answers = generate_from_orthogonal(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;
void _update_input_buffer() {
input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
input_pos = 0;
if (input_len == 0) input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
if (input_pos >= input_len) _update_input_buffer();
return input_buffer[advance ? input_pos++ : input_pos];
}
template <typename T>
inline void read_int(T &number) {
bool negative = false;
number = 0;
while (!isdigit(next_char(false)))
if (next_char() == '-') negative = true;
do {
number = 10 * number + (next_char() - '0');
} while (isdigit(next_char(false)));
if (negative) number = -number;
}
template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
read_int(number);
read_int(args...);
}
} // namespace IO
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
void row_reduce(vector<int64_t> &basis) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
}
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> counts(M + 1, 0);
int64_t value = 0;
for (int mask = 0; mask < 1 << B; mask++) {
counts[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
return counts;
}
vector<int64_t> build_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int> highest_bit(B, -1);
vector<bool> bit_covered(M, false);
for (int i = 0; i < B; i++) {
highest_bit[i] = 63 - __builtin_clzll(basis[i]);
bit_covered[highest_bit[i]] = true;
}
vector<int64_t> orthogonal;
for (int bit = M - 1; bit >= 0; bit--)
if (!bit_covered[bit]) {
int64_t value = 1LL << bit;
for (int i = 0; i < B; i++)
value |= (basis[i] >> bit & 1) << highest_bit[i];
orthogonal.push_back(value);
}
return orthogonal;
}
vector<int64_t> generate_from_orthogonal(vector<int64_t> basis, int M) {
row_reduce(basis);
int B = basis.size();
vector<int64_t> orthogonal = build_orthogonal(basis, M);
vector<int64_t> orthogonal_counts = generate_all(orthogonal, M);
vector<int64_t> counts(M + 1, 0);
vector<vector<int64_t>> choose(M + 1);
for (int n = 0; n <= M; n++) {
choose[n].resize(n + 1);
choose[n][0] = choose[n][n] = 1;
for (int r = 1; r < n; r++)
choose[n][r] = choose[n - 1][r - 1] + choose[n - 1][r];
}
auto &&get_choose = [&](int n, int r) -> int64_t {
if (r < 0 || r > n) return 0;
return choose[n][r];
};
for (int c = 0; c <= M; c++) {
vector<int64_t> w(M + 1, 0);
for (int d = 0; d <= M; d++)
for (int j = 0; j <= d; j++)
w[d] +=
(j % 2 == 0 ? 1 : -1) * get_choose(d, j) * get_choose(M - d, c - j);
for (int d = 0; d <= M; d++) counts[c] += orthogonal_counts[d] * w[d];
assert((counts[c] & ((1LL << (M - B)) - 1)) == 0);
counts[c] >>= M - B;
}
return counts;
}
int main() {
int N, M;
IO::read_int(N, M);
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
IO::read_int(a);
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
vector<int64_t> answers;
if (B <= M / 2)
answers = generate_all(basis, M);
else
answers = generate_from_orthogonal(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> answers(M + 1, 0);
int64_t value = 0;
if (B < 6) {
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
} else {
static const int JUMP = 32;
for (int64_t mask = 0; mask < 1LL << B; mask += JUMP) {
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[3];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[4];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[3];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctzll(mask + JUMP)];
}
}
return answers;
}
vector<int64_t> dp_on_repeats(vector<int64_t> basis, int M) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
}
sort(options.begin(), options.end());
vector<vector<mod_int>> dp(count_sum + 1, vector<mod_int>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
vector<int64_t> answers(M + 1, 0);
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
answers[count + __builtin_popcount(mask)] += (int64_t)dp[count][mask];
return answers;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(basis_obj.basis, basis_obj.basis + B);
vector<int64_t> answers =
B <= 0.61 * M ? generate_all(basis, M) : dp_on_repeats(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
long long n, m, a[1100005], cntA, A[1100005], C[105][105], G[105][105],
ans[105], S[1100005], E[1100005], cnt[1100005];
long long read() {
char c = getchar();
long long ans = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar();
return ans;
}
void Write(long long x) {
if (x < 10)
putchar(x ^ 48);
else
Write(x / 10), putchar((x % 10) ^ 48);
return;
}
long long add(long long x, long long y) {
return (x += y) >= 998244353 ? x - 998244353 : x;
}
long long power(long long x, long long y) {
long long ans = 1, now = x;
if (y < 0) y += 998244353 - 1;
for (register long long i = y; i; i >>= 1, now = now * now % 998244353)
if (i & 1) ans = ans * now % 998244353;
return ans;
}
void Insert(long long x) {
for (register long long i = m - 1; i >= 0; i--) {
if (!(x >> i & 1)) continue;
if (!A[i]) {
A[i] = x, ++cntA;
for (register long long j = 0; j < i; j++)
if (A[i] >> j & 1) A[i] ^= A[j];
for (register long long j = i + 1; j < m; j++)
if (A[j] >> i & 1) A[j] ^= A[i];
break;
} else
x ^= A[i];
}
return;
}
void DFS(int x, long long now) {
if (x > *S)
++ans[cnt[now & 1048575] + cnt[now >> 20 & 1048575] + cnt[now >> 40]];
else
DFS(x + 1, now), DFS(x + 1, now ^ S[x]);
return;
}
int main() {
n = read(), m = read();
for (register long long i = 1; i <= n; i++) Insert(a[i] = read());
for (register int i = 0; i < (1 << 20); i++) cnt[i] = cnt[i >> 1] + (i & 1);
for (register long long i = 0; i < 100; i++) {
C[i][0] = C[i][i] = 1;
for (register long long j = 1; j < 100; j++)
C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
}
if (cntA <= m / 2) {
long long mul = power(2, n - cntA);
for (register long long i = 0; i < m; i++)
if (A[i]) S[++*S] = A[i];
DFS(1, 0);
for (register long long i = 0; i <= m; i++)
Write(ans[i] * mul % 998244353), putchar(' ');
return 0;
}
for (register long long i = 0; i < m; i++) {
if (A[i]) continue;
long long now = 1ll << i;
for (register int j = i + 1; j < m; j++)
if (A[j] >> i & 1) now |= (1ll << j);
S[++*S] = now;
}
DFS(1, 0);
for (register long long i = 0; i <= m; i++)
for (register long long j = 0; j <= m; j++)
for (register long long k = 0; k <= j && k <= i; k++)
G[i][j] = (G[i][j] + C[j][k] * C[m - j][i - k] % 998244353 *
((k & 1) ? 998244353 - 1 : 1)) %
998244353;
for (register long long i = 0; i <= m; i++, putchar(' ')) {
long long Ans = 0;
for (register long long j = 0; j <= m; j++)
Ans = (Ans + ans[j] * G[i][j]) % 998244353;
Write(Ans * power(2, n - m) % 998244353);
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int M = 55, P = 998244353;
int n, m, c[M][M], g[M][M];
long long b[M];
vector<long long> B, G;
int f[60], p[60];
long long pw(long long a, long long m) {
long long res = 1;
while (m) m & 1 ? res = res * a % P : 0, a = a * a % P, m >>= 1;
return res;
}
void insert(long long x) {
for (int i = 53; i >= int(0); --i)
if (x >> i & 1) {
if (!b[i]) return b[i] = x, void();
x ^= b[i];
}
assert(x == 0);
}
void add(int &x, long long y) { x = (x + y) % P; }
void add(int &x, int y) { x = (x + y) % P; }
void work() {
for (int i = 53; i >= int(0); --i)
if (b[i]) {
for (int j = i + 1; j <= int(53); ++j)
if (b[j] >> i & 1) b[j] ^= b[i];
}
for (int i = 0; i <= int(53); ++i)
if (b[i]) B.push_back(b[i]);
}
void dfs(int k, long long cur) {
if (k == 0) {
add(p[__builtin_popcountll(cur)], 1);
return;
}
dfs(k - 1, cur);
dfs(k - 1, cur ^ B[k - 1]);
}
void solve1() {
dfs(B.size(), 0);
long long coef = pw(2, n - B.size());
for (int x = 0; x <= int(m); ++x) p[x] = p[x] * coef % P;
for (int i = 0; i <= int(m); ++i) printf("%d%c", p[i], " \n"[i == m]);
}
void dfs2(int k, long long cur) {
if (k == 0) {
add(f[__builtin_popcountll(cur)], 1);
return;
}
dfs2(k - 1, cur);
dfs2(k - 1, cur ^ G[k - 1]);
}
void solve2() {
c[0][0] = 1;
for (int i = 1; i <= int(m); ++i) {
c[i][0] = 1;
for (int j = 1; j <= int(m); ++j)
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
}
auto sig = [](int x) { return 1 - 2 * (x & 1); };
for (int x = 0; x <= int(m); ++x)
for (int y = 0; y <= int(m); ++y) {
for (int z = 0; z <= int(min(x, y)); ++z) {
add(g[x][y], c[y][z] * 1ll * c[m - y][x - z] * sig(z));
}
add(g[x][y], P);
}
for (int i = 0; i <= int(m - 1); ++i)
if (b[i] == 0) {
long long s = 1ll << i;
for (int j = 0; j <= int(m - 1); ++j)
if (b[j] >> i & 1) s |= 1ll << j;
if (s) G.push_back(s);
}
dfs2(G.size(), 0);
long long coef = pw(pw(2, m - B.size()), P - 2);
for (int x = 0; x <= int(m); ++x) {
for (int y = 0; y <= int(m); ++y) {
add(p[x], f[y] * 1ll * g[x][y]);
}
p[x] = p[x] * coef % P;
}
coef = pw(2, n - B.size());
for (int x = 0; x <= int(m); ++x) p[x] = p[x] * coef % P;
for (int i = 0; i <= int(m); ++i) printf("%d%c", p[i], " \n"[i == m]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= int(n); ++i) {
long long x;
scanf("%lld", &x);
insert(x);
}
work();
if (B.size() <= 26)
solve1();
else
solve2();
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxm = 54;
int n, m, k;
long long first[maxm], a[maxm], b[maxm], p[maxm];
long long c[maxm][maxm];
int ipow(long long b, int e) {
long long ret = 1;
for (; e; e >>= 1) {
if (e & 1) ret = ret * b % mod;
b = b * b % mod;
}
return ret;
}
void dfs(int i, long long x) {
if (i == k)
p[__builtin_popcountll(x)]++;
else
dfs(i + 1, x), dfs(i + 1, x ^ a[i]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
for (int j = m - 1; j >= 0; j--) {
if ((x >> j) & 1) {
if (!first[j]) {
first[j] = x;
break;
}
x ^= first[j];
}
}
}
for (int i = 0; i < m; i++) {
if (first[i]) {
for (int j = 0; j < i; j++) {
first[i] ^= ((first[i] >> j) & 1) * first[j];
}
for (int j = 0; j < m; j++) {
if (!first[j]) a[k] = (a[k] << 1) | ((first[i] >> j) & 1);
}
a[k] |= 1ll << (m - 1 - k), k++;
}
}
if (k < maxm / 2) {
dfs(0, 0);
int x = ipow(2, n - k);
for (int i = 0; i <= m; i++) {
cout << (x * p[i] % mod);
if (i != m) cout << " ";
}
cout << '\n';
} else {
swap(a, b);
k = m - k;
for (int i = 0; i < k; i++) {
for (int j = 0; j < m - k; j++) {
a[i] |= ((b[j] >> i) & 1) << j;
}
a[i] |= 1ll << (m - 1 - i);
}
for (int i = 0; i <= m; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
dfs(0, 0);
int x = (long long)ipow((mod + 1) / 2, k) * ipow(2, n - m + k) % mod;
for (int i = 0; i <= m; i++) {
long long ret = 0;
for (int j = 0; j <= m; j++) {
long long cur = 0;
for (int l = 0; l <= i; l++) {
(cur += (1 - 2 * (l & 1)) * c[j][l] % mod * c[m - j][i - l] % mod +
mod) %= mod;
}
(ret += cur * p[j] % mod) %= mod;
}
cout << (x * ret % mod);
if (i != m) cout << " ";
}
cout << '\n';
}
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return x;
}
const int Mod = 998244353, inv2 = Mod + 1 >> 1, MAXW = (1 << 18) - 1;
int upd(int x) { return x + (x >> 31 & Mod); }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = 1LL * x * x % Mod)
if (y & 1) res = 1LL * res * x % Mod;
return res;
}
int n, m;
long long w[200010], lb[60], Atot;
long long bin[60];
int bintop;
int p[60], q[60], ans[60];
int ins(long long x) {
for (int i = m - 1; i >= 0; i--)
if (x >> i & 1) {
if (lb[i]) {
x ^= lb[i];
continue;
}
lb[i] = x;
for (int j = i - 1; j >= 0; j--)
if (lb[i] >> j & 1) lb[i] ^= lb[j];
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) lb[j] ^= lb[i];
return 1;
}
return 0;
}
void dfs(int *p) {
static long long h[1 << 10];
static int popcnt[1 << 18];
for (int i = 1; i <= MAXW; i++) popcnt[i] = popcnt[i >> 1] + (i & 1);
for (int i = 0; i < bintop; i++) h[(1 << i) % 1007] = bin[i + 1];
long long msk = 0;
p[0]++;
for (int i = 1; i < (1 << bintop); i++) {
msk ^= h[(i & -i) % 1007];
p[popcnt[msk & MAXW] + popcnt[msk >> 18 & MAXW] +
popcnt[msk >> 36 & MAXW]]++;
}
}
int dp[60][60];
int C[60][60];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) Atot += ins(w[i] = read());
if (Atot <= 26) {
for (int i = 0; i < m; i++)
if (lb[i]) bin[++bintop] = lb[i];
dfs(p);
int t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
for (int i = 0; i < m; i++)
if (!lb[i]) {
long long x = 1LL << i;
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) x |= 1LL << j;
bin[++bintop] = x;
}
dfs(q);
for (int i = 0; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
for (int k = 0, t = 1; k <= i && k <= j; k++, t = Mod - t) {
dp[i][j] = (dp[i][j] + 1LL * t * C[j][k] % Mod * C[m - j][i - k]) % Mod;
}
}
for (int c = 0; c <= m; c++)
for (int i = 0; i <= m; i++) p[c] = (p[c] + 1LL * q[i] * dp[c][i]) % Mod;
int t = qpow(inv2, m - Atot);
for (int i = 0; i <= m; i++) p[i] = 1LL * p[i] * t % Mod;
t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const int MAXM = 60;
int N, M;
int B;
vector<long long> basis;
int nc[MAXM];
long long ans[MAXM];
void gogo() {
long long cv = 0;
for (int i = 1; i < (1 << B); i++) {
nc[__builtin_popcountll(cv)]++;
cv ^= basis[__builtin_ctz(i)];
}
nc[__builtin_popcountll(cv)]++;
}
int getp(long long x) { return 63 - __builtin_clzll(x); }
long long pascal[MAXM][MAXM];
int main() {
ios_base::sync_with_stdio(0);
pascal[0][0] = 1;
for (int i = 1; i < MAXM; i++)
for (int j = 0; j < MAXM; j++) {
pascal[i][j] = pascal[i - 1][j];
if (j) pascal[i][j] = (pascal[i][j] + pascal[i - 1][j - 1]) % MOD;
}
cin >> N >> M;
for (int i = 0; i < N; i++) {
long long x;
cin >> x;
for (long long b : basis)
if ((x ^ b) < x) x ^= b;
if (x > 0) {
for (long long &b : basis)
if ((b ^ x) < b) b ^= x;
basis.push_back(x);
sort(basis.begin(), basis.end());
reverse(basis.begin(), basis.end());
}
}
B = basis.size();
if (B * 2 <= M) {
gogo();
long long cmult = 1;
for (int i = 0; i < N - basis.size(); i++) cmult = (cmult * 2) % MOD;
for (int i = 0; i <= M; i++) {
if (i) cout << " ";
cout << (nc[i] * cmult) % MOD;
}
cout << "\n";
return 0;
}
vector<long long> nbasis;
for (int i = 0; i < M; i++) {
bool bfound = false;
for (long long b : basis)
if (getp(b) == i) bfound = true;
if (bfound) continue;
long long nb = (1LL << i);
for (long long b : basis)
if (b & (1LL << i)) nb ^= (1LL << getp(b));
nbasis.push_back(nb);
}
basis = nbasis;
B = basis.size();
gogo();
for (int i = 0; i <= M; i++) {
ans[i] = 0;
for (int j = 0; j <= M; j++) {
for (int k = 0; k <= i; k++) {
if (k > j || i - k > M - j) continue;
int mult = 1;
if (k % 2 == 1) mult = -1;
ans[i] += ((nc[j] * pascal[j][k]) % MOD) * pascal[M - j][i - k] * mult;
ans[i] = (ans[i] % MOD + MOD) % MOD;
}
}
}
long long cmult = 1;
for (int i = 0; i < N - M; i++) cmult = (cmult * 2) % MOD;
for (int i = 0; i < M - N; i++) cmult = (cmult * (MOD + 1) / 2) % MOD;
for (int i = 0; i <= M; i++) {
if (i) cout << " ";
cout << (ans[i] * cmult) % MOD;
}
cout << "\n";
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int RLEN = 1 << 20 | 1;
inline char gc() {
static char ibuf[RLEN], *ib, *ob;
(ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
return (ib == ob) ? EOF : *ib++;
}
inline int read() {
char ch = gc();
int res = 0;
bool f = 1;
while (!isdigit(ch)) f ^= ch == '-', ch = gc();
while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc();
return f ? res : -res;
}
inline long long readll() {
char ch = gc();
long long res = 0;
bool f = 1;
while (!isdigit(ch)) f ^= ch == '-', ch = gc();
while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc();
return f ? res : -res;
}
inline int readstring(char *s) {
int top = 0;
char ch = gc();
while (isspace(ch)) ch = gc();
while (!isspace(ch) && ch != EOF) s[++top] = ch, ch = gc();
s[top + 1] = '\0';
return top;
}
template <typename tp>
inline void chemx(tp &a, tp b) {
a = max(a, b);
}
template <typename tp>
inline void chemn(tp &a, tp b) {
a = min(a, b);
}
const int mod = 998244353;
inline int add(int a, int b) {
return (a + b) >= mod ? (a + b - mod) : (a + b);
}
inline int dec(int a, int b) { return (a < b) ? (a - b + mod) : (a - b); }
inline int mul(int a, int b) {
static long long r;
r = (long long)a * b;
return (r >= mod) ? (r % mod) : r;
}
inline void Add(int &a, int b) { a = (a + b) >= mod ? (a + b - mod) : (a + b); }
inline void Dec(int &a, int b) { a = (a < b) ? (a - b + mod) : (a - b); }
inline void Mul(int &a, int b) {
static long long r;
r = (long long)a * b;
a = (r >= mod) ? (r % mod) : r;
}
inline int ksm(int a, int b, int res = 1) {
for (; b; b >>= 1, Mul(a, a)) (b & 1) && (Mul(res, a), 1);
return res;
}
inline int Inv(int x) { return ksm(x, mod - 2); }
inline int fix(long long x) {
x %= mod;
return (x < 0) ? x + mod : x;
}
const int N = 200005, M = 66;
long long bas[M], a[M], b[M];
int n, m, k, sz, p[M], C[M][M];
inline void init_C() {
for (int i = 0; i < M; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
}
}
void dfs(int pos, long long res) {
if (pos >= k) {
p[__builtin_popcountll(res)]++;
return;
}
dfs(pos + 1, res ^ a[pos]), dfs(pos + 1, res);
}
void dfs2(int pos, long long res) {
if (pos >= m - k) {
{
p[__builtin_popcountll(res)]++;
return;
}
}
dfs2(pos + 1, res ^ b[pos]), dfs2(pos + 1, res);
}
inline void solve2() {
for (int i = 0; i < m - k; i++) {
long long res = 0;
for (int j = 0; j < k; j++)
if (a[j] & (1ll << i)) res ^= 1ll << j;
b[i] = res | (1ll << (m - 1 - i));
}
dfs2(0, 0);
for (int i = 0; i <= m; i++) {
int res = 0;
for (int j = 0; j <= m; j++) {
int c = 0;
for (int k = 0; k <= i; k++) {
int now = mul(C[m - j][i - k], C[j][k]);
if (k & 1)
Dec(c, now);
else
Add(c, now);
}
Add(res, mul(c, p[j]));
}
cout << mul(res, mul(ksm(2, n - k), Inv(ksm(2, m - k)))) << " ";
}
}
inline void solve1() {
dfs(0, 0);
for (int i = 0; i <= m; i++) cout << mul(p[i], ksm(2, n - k)) << " ";
exit(0);
}
inline void insert(long long x) {
for (int i = m - 1; ~i; i--)
if ((x >> i) & 1) {
if (!bas[i]) {
sz++, bas[i] = x;
return;
}
x ^= bas[i];
}
}
int main() {
init_C();
n = read(), m = read();
for (int i = 1; i <= n; i++) insert(readll());
for (int i = 0; i < m; i++)
if (bas[i]) {
for (int j = 0; j < i; j++)
if (bas[j] && ((bas[i] >> j) & 1)) bas[i] ^= bas[j];
long long x = 0;
for (int j = 0; j < m; j++)
if (!bas[j]) {
x <<= 1, x += (bas[i] >> j) & 1;
}
a[k] = x + (1ll << (m - 1 - k)), k++;
}
if (k <= 26)
solve1();
else
solve2();
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, num = 1, rnk, k, ans[100], res[100], C[100][100];
long long b[100], a[100], c[100];
void insert(long long x) {
for (int i = m - 1; ~i; i--)
if (x >> i & 1) {
if (b[i])
x ^= b[i];
else {
for (int j = i - 1; j >= 0; j--)
if (b[j] && x >> j & 1) x ^= b[j];
b[i] = x, rnk++;
for (int j = i + 1; j < m; j++)
if (b[j] >> i & 1) b[j] ^= x;
return;
}
}
num = 2ll * num % mod;
}
void dfs(int x, long long y) {
if (x > k) {
ans[__builtin_popcountll(y)]++;
return;
}
dfs(x + 1, y);
dfs(x + 1, y ^ a[x]);
}
int qpow(int a, int b) {
int c = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) c = 1ll * a * c % mod;
return c;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
insert(x);
}
if (rnk <= 27) {
for (int i = 0; i < m; i++)
if (b[i]) a[++k] = b[i];
dfs(1, 0);
} else {
for (int i = 0; i < m; i++)
if (!b[i]) {
a[++k] |= 1ll << i;
for (int j = 0; j < m; j++)
if (b[j] >> i & 1) {
a[k] |= 1ll << j;
}
}
dfs(1, 0);
for (int i = 0; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
for (int i = 0; i <= m; i++) {
int cur = 1;
for (int j = 0; j <= i; j++) {
for (int k = 0; k <= m - i; k++) {
res[j + k] = (res[j + k] + 1ll * ans[i] * cur % mod * C[i][j] % mod *
C[m - i][k]) %
mod;
}
cur = mod - cur;
}
}
int x = qpow(2, mod - 1 - (m - rnk));
for (int i = 0; i <= m; i++) res[i] = 1ll * res[i] * x % mod;
swap(ans, res);
}
for (int i = 0; i <= m; i++) ans[i] = 1ll * ans[i] * num % mod;
for (int i = 0; i <= m; i++) printf("%d%c", ans[i], " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
vector<int64_t> generate_all(vector<int64_t> basis, int M) {
int B = basis.size();
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
vector<int64_t> answers(M + 1, 0);
int64_t value = 0;
if (B < 6) {
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
} else {
static const int JUMP = 32;
for (int64_t mask = 0; mask < 1LL << B; mask += JUMP) {
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[3];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[4];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[3];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[2];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[1];
answers[__builtin_popcountll(value)]++;
value ^= change[0];
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctzll(mask + JUMP)];
}
}
return answers;
}
vector<int64_t> dp_on_repeats(vector<int64_t> basis, int M) {
int B = basis.size();
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
}
sort(options.begin(), options.end());
vector<vector<mod_int>> dp(count_sum + 1, vector<mod_int>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
vector<int64_t> answers(M + 1, 0);
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
answers[count + __builtin_popcount(mask)] += (int64_t)dp[count][mask];
return answers;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
int B = basis_obj.n;
vector<int64_t> basis(basis_obj.basis, basis_obj.basis + B);
vector<int64_t> answers =
B <= 0.59 * M ? generate_all(basis, M) : dp_on_repeats(basis, M);
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return x;
}
const int Mod = 998244353, inv2 = Mod + 1 >> 1, MAXW = (1 << 18) - 1;
int upd(int x) { return x + (x >> 31 & Mod); }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = 1LL * x * x % Mod)
if (y & 1) res = 1LL * res * x % Mod;
return res;
}
int n, m;
long long w[200010], lb[60], Atot;
long long bin[60];
int bintop;
int p[60], q[60], ans[60];
int ins(long long x) {
for (int i = m - 1; i >= 0; i--)
if (x >> i & 1) {
if (lb[i]) {
x ^= lb[i];
continue;
}
lb[i] = x;
for (int j = i - 1; j >= 0; j--)
if (lb[i] >> j & 1) lb[i] ^= lb[j];
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) lb[j] ^= lb[i];
return 1;
}
return 0;
}
void dfs(int *p) {
for (int i = 0; i < bintop; i++) bin[i] = bin[i + 1];
long long msk = 0;
p[0]++;
for (int i = 1; i < (1 << bintop); i++) {
msk ^= bin[__builtin_ctz(i)];
p[__builtin_popcountll(msk)]++;
}
}
int dp[60][60];
int C[60][60];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) Atot += ins(w[i] = read());
if (Atot <= m - Atot) {
for (int i = 0; i < m; i++)
if (lb[i]) bin[++bintop] = lb[i];
dfs(p);
int t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
for (int i = 0; i < m; i++)
if (!lb[i]) {
long long x = 1LL << i;
for (int j = i + 1; j < m; j++)
if (lb[j] >> i & 1) x |= 1LL << j;
bin[++bintop] = x;
}
dfs(q);
for (int i = 0; i <= m; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod;
}
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++) {
for (int k = 0, t = 1; k <= i && k <= j; k++, t = Mod - t) {
dp[i][j] = (dp[i][j] + 1LL * t * C[j][k] % Mod * C[m - j][i - k]) % Mod;
}
}
for (int c = 0; c <= m; c++)
for (int i = 0; i <= m; i++) p[c] = (p[c] + 1LL * q[i] * dp[c][i]) % Mod;
int t = qpow(inv2, m - Atot);
for (int i = 0; i <= m; i++) p[i] = 1LL * p[i] * t % Mod;
t = qpow(2, n - Atot);
for (int i = 0; i <= m; i++)
printf("%d%c", ans[i] = 1LL * p[i] * t % Mod, " \n"[i == m]);
return 0;
}
| CPP |
1336_E2. Chiori and Doll Picking (hard version) | This is the hard version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved.
Chiori loves dolls and now she is going to decorate her bedroom!
<image>
As a doll collector, Chiori has got n dolls. The i-th doll has a non-negative integer value a_i (a_i < 2^m, m is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are 2^n different picking ways.
Let x be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls x = 0). The value of this picking way is equal to the number of 1-bits in the binary representation of x. More formally, it is also equal to the number of indices 0 β€ i < m, such that \leftβ (x)/(2^i) \rightβ is odd.
Tell her the number of picking ways with value i for each integer i from 0 to m. Due to the answers can be very huge, print them by modulo 998 244 353.
Input
The first line contains two integers n and m (1 β€ n β€ 2 β
10^5, 0 β€ m β€ 53) β the number of dolls and the maximum value of the picking way.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^m) β the values of dolls.
Output
Print m+1 integers p_0, p_1, β¦, p_m β p_i is equal to the number of picking ways with value i by modulo 998 244 353.
Examples
Input
4 4
3 5 8 14
Output
2 2 6 6 0
Input
6 7
11 45 14 9 19 81
Output
1 2 11 20 15 10 5 0 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
string sep;
for (const auto &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator int64_t() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const _m_int &other) const { return val == other.val; }
bool operator!=(const _m_int &other) const { return val != other.val; }
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
const int BITS = 53;
template <typename T>
struct xor_basis {
T basis[BITS];
int n = 0;
T min_value(T start) const {
if (n == BITS) return 0;
for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);
return start;
}
T max_value(T start = 0) const {
if (n == BITS) return ((T)1 << BITS) - 1;
for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);
return start;
}
bool add(T x) {
x = min_value(x);
if (x == 0) return false;
basis[n++] = x;
for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)
swap(basis[k], basis[k - 1]);
return true;
}
void merge(const xor_basis<T> &other) {
for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);
}
void merge(const xor_basis<T> &a, const xor_basis<T> &b) {
if (a.n > b.n) {
*this = a;
merge(b);
} else {
*this = b;
merge(a);
}
}
};
int N, M, B;
vector<int64_t> basis;
vector<int64_t> answers;
void generate_all() {
vector<int64_t> change(B + 1, 0);
int64_t prefix = 0;
for (int i = 0; i < B; i++) {
prefix ^= basis[i];
change[i] = prefix;
}
change[B] = prefix;
int64_t value = 0;
for (unsigned mask = 0; mask < 1U << B; mask++) {
answers[__builtin_popcountll(value)]++;
value ^= change[__builtin_ctz(mask + 1)];
}
}
void dp_on_repeats() {
;
for (int i = 0; i < B; i++)
for (int j = i + 1; j < B; j++)
basis[i] = min(basis[i], basis[i] ^ basis[j]);
;
vector<int> repeats;
for (int bit = 0; bit < M; bit++) {
int count = 0;
for (int i = 0; i < B; i++)
if (basis[i] >> bit & 1) count++;
if (count > 1) repeats.push_back(bit);
}
int R = repeats.size();
vector<pair<int, int>> options(B);
int count_sum = 0;
for (int i = 0; i < B; i++) {
int64_t value = basis[i];
int count = __builtin_popcountll(value);
int repeat_mask = 0;
for (int r = 0; r < R; r++)
if (value >> repeats[r] & 1) {
count--;
repeat_mask |= 1 << r;
}
options[i] = {count, repeat_mask};
count_sum += count;
};
sort(options.begin(), options.end());
vector<vector<mod_int>> dp(count_sum + 1, vector<mod_int>(1 << R, 0));
dp[0][0] = 1;
int max_count = 0;
for (pair<int, int> &option : options) {
int opt_count = option.first;
int opt_mask = option.second;
for (int count = max_count; count >= 0; count--)
for (int mask = 0; mask < 1 << R; mask++)
dp[count + opt_count][mask ^ opt_mask] += dp[count][mask];
max_count += opt_count;
}
for (int count = 0; count <= count_sum; count++)
for (int mask = 0; mask < 1 << R; mask++)
answers[count + __builtin_popcount(mask)] += (int)dp[count][mask];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
xor_basis<int64_t> basis_obj;
for (int i = 0; i < N; i++) {
int64_t a;
cin >> a;
basis_obj.add(a);
}
B = basis_obj.n;
basis.resize(B);
for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i];
answers.assign(M + 1, 0);
if (B <= 0.6 * M)
generate_all();
else
dp_on_repeats();
for (int i = 0; i <= M; i++) {
mod_int answer = answers[i];
answer *= mod_int(2).pow(N - B);
cout << answer << (i < M ? ' ' : '\n');
}
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | # abs((desired*(2*n + 1) - ((n+1)*hot + n*cold))/(2*n + 1))
#EXPERIMENTING WITH LOSS DEFINTION - float ops ko aage peeche kar diya
import sys
def input():
return sys.stdin.readline().rstrip()
testcases = int(input())
answers = []
def loss(two_n_plus_one, hot, cold, desired):
n = two_n_plus_one//2
# if n == 0:
# return float('inf')
# else:
# return abs(desired - ((n+1)*hot + n*cold)/(2*n + 1))
return abs((desired*(2*n + 1) - ((n+1)*hot + n*cold))/(2*n + 1))
for _ in range(testcases):
hot, cold, desired_temp = [int(i) for i in input().split()]
#required number of cups to get it to desired_temp
mid_way = (hot + cold)/2
if hot == cold:
answers.append(1)
elif desired_temp >= hot:
answers.append(1)
elif desired_temp <= mid_way:
answers.append(2)
else:
#find n, -> num iters
#n + 1 hots, n colds
frac = (hot - desired_temp) / (desired_temp - mid_way)
frac /= 2
# option1 = int(frac)
option1 = 2*(int(frac)) + 1
option2 = option1 + 2
l1 , l2 = loss(option1, hot, cold, desired_temp),loss(option2, hot, cold, desired_temp)
if min(l1, l2) >= (hot - desired_temp) and (hot -desired_temp) <= (desired_temp - mid_way):
answers.append(1)
elif min(l1, l2) >= (desired_temp - mid_way):
answers.append(2)
elif l1 <= l2:
answers.append(option1)
else:
answers.append(option2)
# if option1%2 == 0:
# option1 += 1 #taki odd ho jaye
# option2 = option1 - 2
# option3 = option1 + 2
# l1, l2, l3 = loss(option1, hot, cold, desired_temp),loss(option2, hot, cold, desired_temp),loss(option3, hot, cold, desired_temp)
# option4 = option1 - 1
# answers.append(min(loss(option1, hot, cold, desired_temp),
# loss(option2, hot, cold, desired_temp),
# loss(option3, hot, cold, desired_temp)))
print(*answers, sep = '\n') | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | # Nilfer
for _ in range(int(input())):
p,a,t=map(int,input().split())
if t<=(p+a)/2:print(2)
else:
k=(p-t)//(2*t-p-a)
if abs((2*k+3)*t-k*p-2*p-k*a-a)*(2*k+1)<abs((2*k+1)*t-k*p-p-k*a)*(2*k+3):print(2*k+3)
else:print(2*k+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | eps = 1e-10
t = int(input())
for _ in range(t):
h, c, t = map(int, input().split())
l = 0
r = 100000000
while l + 1 < r:
mid = (l + r) // 2
if mid % 2 == 0:
if mid + 1 < r:
mid += 1
elif mid - 1 > l:
mid -= 1
else:
break
p = (mid // 2 + 1) * h + (mid // 2) * c
if (p / mid) - t > -eps:
l = mid
else:
r = mid
r = l + 2
for_2 = abs(((h + c) / 2 - t) * l * r)
for_l = abs(((l // 2 + 1) * h + (l // 2) * c) * r - t * l * r)
for_r = abs(((r // 2 + 1) * h + (r // 2) * c) * l - t * l * r)
m = min(for_2, min(for_l, for_r))
#print(for_l, for_r, for_2)
if abs(m - for_2) < eps:
print(2)
elif abs(m - for_l) < eps:
print(l)
else:
print(r)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | tt=int(input())
while(tt):
h,c,t=map(int,input().split())
if h == t:
print(1)
elif t <= (h + c) / 2:
print(2)
else:
k = (t - c - 1) // (2 * t - h - c)
ans = 2 * k + 1
if(((4 * k * k - 1) * (2 * t - h - c)) >= (2 * (h - c) * k)):
ans -= 2
print(ans)
tt-=1
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import math
T = int(input())
for test in range(T):
h, c, t = map(int, input().split())
if h + c - 2 * t >= 0:
print(2)
elif h <= t:
print(1)
else:
x = int((h - t) / (2 * t - h - c))
xp1 = x + 1
if abs((h + c) * x + h - 2 * x * t - t) * (2 * xp1 + 1) <= abs((h + c) * xp1 + h - 2 * xp1 * t - t) * (2 * x + 1):
print(2 * x + 1)
else:
print(2 * xp1 + 1)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for _ in range(int(input())):
h,c,t=[float(i) for i in raw_input().split()]
if t<=(h+c)/2:
print(2)
else:
val=(t-c)/(2*t-h-c)
x1=int(val)
diff=h*x1+c*(x1-1)
diff=abs(t*(2*x1-1)-diff)
x1+=1
diff2=h*x1+c*(x1-1)
diff2=abs(t*(2*x1-1)-diff2)
if (2*x1-1)*diff>diff2*(2*x1-3):
print(2*x1-1)
else:
print(2*x1-3)
| PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now----------------------------------------------------
for t in range (int(input())):
a,b,c=map(int,input().split())
c1=0
c2=0
c3=0
c1=(a+b)/2
d1=abs(c1-c)
r=2
d2=c
d3=c
if d1!=0:
x=(a-c)/(2*c-a-b)
x=math.floor(x)
c2=Fraction((a*(x+1)+b*x),(2*x+1))
d2=abs(c2-c)
y=x+1
c3=Fraction((a*(y+1)+b*y),(2*y+1))
d3=abs(c3-c)
z=0
c4=0
d4=c
if x>0:
z=x-1
c4=Fraction((a*(z+1)+b*z),(2*z+1))
d4=abs(c4-c)
l=list()
l.append((d1,2))
l.append((d2,2*x+1))
l.append((d3,2*y+1))
l.append((d4,2*z+1))
l.sort()
r=l[0][1]
#print(l[0][0])
if c<=c1:
r=2
if c==a:
r=1
print(r) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')
from decimal import Decimal
eps = 0
def get_diff(x,h,c,t):
return abs(t - ((x*h + (x-1)*c) / (2*x-1)))
for _ in xrange(int(input())):
h,c,t = (float(x) for x in input().split())
left = 0
right = h
diff = 0
if t == h:
print 1
elif t <= (h+c) / 2:
print 2
else:
t = Decimal(t)
c = Decimal(c)
h= Decimal(h)
x = int((c-t)/(h+c-2*t))
if x > 0:
diffa = get_diff(x,h,c,t)
else:
diffa = h*h
if x-1 > 0:
diffb = get_diff(x-1,h,c,t)
else:
diffb = h*h
diffc = get_diff(x+1,h,c,t)
if diffb <= diffa and diffb <= diffc:
ans = x - 1
elif diffa <= diffb and diffa <= diffc:
ans = x
else:
ans = x + 1
print 2*ans - 1
| PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for _ in range(int(input())):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
continue
x = (h - t) // (2*t - h - c)
k = 2*x + 1
val1 = abs(k//2 * c + (k+1)//2 * h - t*k)
val2 = abs((k+2)//2 * c + (k+3)//2 * h - t*(k+2))
print(k if val1 * (k + 2) <= val2 * k else k + 2) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | //package learning;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class NitsLocal {
static ArrayList<String> s1;
static boolean[] prime;
static int n = 200001;
static void sieve() {
Arrays.fill(prime , true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k<= n ; k+=i) {
prime[k] = false;
}
}
}
}
public static void main(String[] args) {
InputReader sc = new InputReader(System.in);
//prime = new boolean[n + 1];
// sieve();
//prime[1] = false;
/*
int n = sc.ni();
int k = sc.ni();
int []vis = new int[n+1];
int []a = new int[k];
for(int i=0;i<k;i++)
{
a[i] = i+1;
}
int j = k+1;
int []b = new int[n+1];
for(int i=1;i<=n;i++)
{
b[i] = -1;
}
int y = n - k +1;
int tr = 0;
int y1 = k+1;
while(y-- > 0)
{
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
b[pos] = a1;
for(int i=0;i<k;i++)
{
if(a[i] == pos)
{
a[i] = y1;
y1++;
}
}
}
ArrayList<Integer> a2 = new ArrayList<>();
if(y >= k)
{
int c = 0;
int k1 = 0;
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
c++;
a[k1] = i;
a2.add(b[i]);
k1++;
}
if(c==k)
break;
}
Collections.sort(a2);
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
int ans = -1;
for(int i=0;i<a2.size();i++)
{
if(a2.get(i) == a1)
{
ans = i+1;
break;
}
}
System.out.println("!" + " " + ans);
System.out.flush();
}
else
{
int k1 = 0;
a = new int[k];
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
a[k1] = i;
a2.add(b[i]);
k1++;
}
}
for(int i=1;i<=n;i++)
{
if(b[i] == -1)
{
a[k1] = i;
k1++;
if(k1==k)
break;
}
}
int ans = -1;
while(true)
{
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
int f = 0;
if(b[pos] != -1)
{
Collections.sort(a2);
for(int i=0;i<a2.size();i++)
{
if(a2.get(i) == a1)
{
ans = i+1;
f = 1;
System.out.println("!" + " " + ans);
System.out.flush();
break;
}
}
if(f==1)
break;
}
else
{
b[pos] = a1;
a = new int[k];
a2.add(a1);
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
a[k1] = i;
a2.add(b[i]);
k1++;
}
}
for(int i=1;i<=n;i++)
{
if(b[i] == -1)
{
a[k1] = i;
k1++;
if(k1==k)
break;
}
}
}
}
*/
/*
int n = sc.ni();
int []a = sc.nia(n);
int []b = sc.nia(n);
Integer []d = new Integer[n];
int []d1 = new int[n];
for(int i=0;i<n;i++)
{
d[i] = (a[i] - b[i]);
}
int l = 0;
int r = n-1;
Arrays.sort(d);
long res = 0;
while(l < r)
{
if(d[l] + d[r] > 0)
{
res += (long) (r-l);
r--;
}
else
l++;
}
w.println(res);
*/
/*
int n = sc.ni();
ArrayList<Integer> t1 = new ArrayList<>();
int []p1 = new int[n+1];
int []q1 = new int[n-1];
int []q2 = new int[n-1];
for(int i=0;i<n-1;i++)
{
int p = sc.ni();
int q = sc.ni();
t1.add(q);
p1[p]++;
q1[i] = p;
q2[i] = q;
}
int res = 0;
for(int i=0;i<t1.size();i++)
{
if(p1[t1.get(i)] == 0)
{
res = t1.get(i);
//break;
}
}
int y = 1;
for(int i=0;i<n-1;i++)
{
if(q2[i] == res)
{
w.println("0");
}
else
{
w.println(y + " ");
y++;
}
}
*/
/*
int n = sc.ni();
int k = sc.ni();
Integer []a = new Integer[n];
for(int i=0;i<n;i++)
{
a[i] = sc.ni();
a[i] = a[i]%k;
}
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=0;i<n;i++)
{
if(!hm.containsKey(a[i]))
hm.put(a[i], 1);
else
hm.put(a[i], hm.get(a[i]) + 1);
}
Arrays.sort(a);
int z = 0;
long res = 0;
HashMap<Integer,Integer> v = new HashMap<>();
for(int i=0;i<n;i++)
{
if(a[i] == 0)
res++;
else
{
if(a[i] == k-a[i] && !v.containsKey(a[i]))
{
res += (long) hm.get(a[i]) / 2;
v.put(a[i],1);
}
else
{
if(hm.containsKey(a[i]) && hm.containsKey(k-a[i]))
{
int temp = a[i];
if(!v.containsKey(a[i]) && !v.containsKey(k-a[i]))
{
res += (long) Math.min(hm.get(temp), hm.get(k-temp));
v.put(temp,1);
v.put(k-temp,1);
}
}
}
}
}
w.println(res);
*/
/*
int []a = new int[4];
//G0
a[0] = 0;
a[1] = 1;
a[2] = 0;
a[3] = 1;
int []b = new int[4];
//g1
b[0] = 0;
b[1] = 1;
b[2] = 1;
b[3] = 0;
int [][]dp1 = new int[4][4];
int [][]dp2 = new int[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
dp1[i][j] = (a[i] | a[j]);
}
// w.println();
}
w.println();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
dp2[i][j] = (b[i] | b[j]);
}
//w.println();
}
int a1 = 0;
int b1 = 0;
int c1 = 0;
int d1 = 0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(dp1[i][j] == 0 && dp2[i][j] == 0)
a1++;
else if(dp1[i][j] == 1 && dp2[i][j] == 1)
b1++;
else if(dp1[i][j] == 0 && dp2[i][j] == 1)
c1++;
else
d1++;
}
}
w.println(a1+ " " + b1 + " " + c1 +" "+ d1);
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q-- > 0)
{
long x = sc.nextLong();
long y = sc.nextLong();
long a = (x - y)/2;
long a1 = 0, b1 = 0;
int lim = 8;
int f = 0;
for (int i=0; i<8*lim; i++)
{
long pi = (y & (1 << i));
long qi = (a & (1 << i));
if (pi == 0 && qi == 0)
{
}
else if (pi == 0 && qi > 0)
{
a1 = ((1 << i) | a1);
b1 = ((1 << i) | b1);
}
else if (pi > 0 && qi == 0)
{
a1 = ((1 << i) | a1);
}
else
{
f = 1;
}
}
if(a1+b1 != x)
f =1;
if(f==1)
System.out.println("-1");
else
{
if(a1 > b1)
{
long temp = a1;
a1 = b1;
b1 = temp;
}
System.out.println(a1 + " " + b1);
}
}
*/
/*
int n = sc.ni();
int k = sc.ni();
int []a = sc.nia(n);
int []c = new int[n+1];
int m = n;
int f = 0;
for(int i=0;i<k;i++)
{
c[a[i]]++;
}
HashSet<Integer> temp = new HashSet<>();
for(int i=k;i<n;i++)
{
if(c[a[i]] == 0)
{
temp.add(a[i]);
}
}
int re = temp.size();
int st = 0;
if(re > 0)
{
for(int i=k-1;i>=0;i--)
{
c[a[i]]--;
if(c[a[i]] == 0)
{
f = 1;
break;
}
else
{
re--;
if(re == 0)
break;
}
}
st = k-temp.size();
}
if(f==1 || st <0)
w.println("-1");
else
{
int div = 10000/k;
int mod = 10000%k;
Iterator itr = temp.iterator();
while(itr.hasNext())
{
Integer e = (Integer) itr.next();
a[st] = e;
st++;
}
w.println("10000");
for(int i=0;i<div;i++)
{
for(int j=0;j<k;j++)
{
w.print(a[j] + " ");
}
}
for(int i=0;i<mod;i++)
{
w.print(a[i] + " ");
}
w.println();
}
*/
int t = sc.ni();
while(t-- > 0)
{
long h = sc.nl();
long c = sc.nl();
long temp = sc.nl();
BigDecimal tempt = new BigDecimal(temp);
BigDecimal first = new BigDecimal(((double)h+(double)c)/(double)2);
BigDecimal diff1 = first.subtract(tempt);
if(diff1.compareTo(BigDecimal.ZERO) < 0)
diff1 = diff1.multiply(new BigDecimal("-1.0"));
double coeff = (double) 2*temp - ((double)h + (double)c);
long cons = h - temp;
if(temp == h)
{
w.println("1");
}
else if(coeff <= 0)
{
w.println("2");
}
else
{
long x1 = (long) Math.floor((double) cons / coeff);
long x2 = (long) Math.ceil((double) cons / coeff);
BigDecimal x11 = new BigDecimal(x1);
BigDecimal x22 = new BigDecimal(x2);
BigDecimal hplusc = new BigDecimal(h+c);
x11 = x11.multiply(hplusc);
x22 = x22.multiply(hplusc);
BigDecimal hh = new BigDecimal(h);
BigDecimal denm = new BigDecimal(x1);
BigDecimal two = new BigDecimal("2");
denm = denm.multiply(two).add(BigDecimal.ONE);
BigDecimal denm1 = new BigDecimal(x2);
denm1 = denm1.multiply(two).add(BigDecimal.ONE);
BigDecimal t1 = (x11.add(hh)).divide(denm,MathContext.DECIMAL128);
BigDecimal diff2 = t1.subtract(tempt);
if(diff2.compareTo(BigDecimal.ZERO) < 0)
diff2 = diff2.multiply(new BigDecimal("-1.0"));
//w.println(denm);
//w.println(denm1);
long ans = 0;
BigDecimal abd = new BigDecimal("-1.0");
if(diff1.compareTo(diff2) <= 0)
{
ans = 2;
if(diff1.compareTo(diff2) == 0 && x1 == 0)
{
ans = 1;
abd = diff2;
}
else
abd = diff1;
}
else
{
ans = (2*x1)+1;
abd = diff2;
}
BigDecimal t2 = x22.add(hh).divide(denm1,MathContext.DECIMAL128);
BigDecimal diff3 = t2.subtract(tempt);
if(diff3.compareTo(BigDecimal.ZERO) < 0)
diff3 = diff3.multiply(new BigDecimal("-1.0"));
if(diff3.compareTo(abd) < 0)
{
ans = (2*x2) + 1;
}
w.println(ans);
}
}
w.close();
}
static int []res;
static int c = 0;
static class Pair{
int fir;
int last;
Pair(int fir,int last)
{
this.fir = fir;
this.last = last;
}
}
static class m1 implements Comparator<Pair>{
public int compare(Pair a, Pair b)
{
int t1 = a.last - a.fir;
int t2 = b.last - b.fir;
if(t1 < t2)
return 1;
else if(t1 > t2)
return -1;
else
{
if(a.fir < b.fir)
return -1;
else
return 1;
}
}
}
static long calc(long x)
{
double res = Math.sqrt(1 + 24*(double)x);
res -= 1.0;
res = res / (double) 6;
return (long) res;
}
public static final BigInteger MOD = BigInteger.valueOf(998244353L);
static BigInteger fact(int x)
{
BigInteger res = BigInteger.ONE;
for(int i=2;i<=x;i++)
{
res = res.multiply(BigInteger.valueOf(i)).mod(MOD);
}
return res;
}
public static long calc(int x,int y,int z)
{
long res = ((long) (x-y) * (long) (x-y)) + ((long) (z-y) * (long) (z-y)) + ((long) (x-z) * (long) (x-z));
return res;
}
public static int upperBound(ArrayList<Integer>arr, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= arr.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
public static int lowerBound(ArrayList<Integer> arr, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
//checks if the value is less than middle element of the array
if (value <= arr.get(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
static HashMap<Long,Integer> map;
static class Coin{
long a;
long b;
Coin(long a, long b)
{
this.a = a;
this.b = b;
}
}
static ArrayList<Long> fac;
static HashSet<Long> hs;
public static void primeFactors(long n)
{
// Print the number of 2s that divide n
while (n % 2 == 0) {
fac.add(2l);
hs.add(2l);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i*i <= n; i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
fac.add(i);
hs.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
fac.add(n);
}
static int smallestDivisor(int n)
{
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
static boolean IsP(String s,int l,int r)
{
while(l <= r)
{
if(s.charAt(l) != s.charAt(r))
{
return false;
}
l++;
r--;
}
return true;
}
static class Student{
int id;
int val;
Student(int id,int val)
{
this.id = id;
this.val = val;
}
}
static int upperBound(ArrayList<Integer> a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(a.get(middle) >= element)
high = middle;
else
low = middle + 1;
}
return low;
}
static long func(long t,long e,long h,long a, long b)
{
if(e*a >= t)
return t/a;
else
{
return e + Math.min(h,(t-e*a)/b);
}
}
public static int countSetBits(int number){
int count = 0;
while(number>0){
++count;
number &= number-1;
}
return count;
}
static long modexp(long x,long n,long M)
{
long power = n;
long result=1;
x = x%M;
while(power>0)
{
if(power % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
power = power/2;
}
return result;
}
static long modInverse(long A,long M)
{
return modexp(A,M-2,M);
}
static long gcd(long a,long b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
static class Temp{
int a;
int b;
int c;
int d;
Temp(int a,int b,int c,int d)
{
this.a = a;
this.b = b;
this.c =c;
this.d = d;
//this.d = d;
}
}
static long sum1(int t1,int t2,int x,int []t)
{
int mid = (t2-t1+1)/2;
if(t1==t2)
return 0;
else
return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t);
}
static String replace(String s,int a,int n)
{
char []c = s.toCharArray();
for(int i=1;i<n;i+=2)
{
int num = (int) (c[i] - 48);
num += a;
num%=10;
c[i] = (char) (num+48);
}
return new String(c);
}
static String move(String s,int h,int n)
{
h%=n;
char []c = s.toCharArray();
char []temp = new char[n];
for(int i=0;i<n;i++)
{
temp[(i+h)%n] = c[i];
}
return new String(temp);
}
public static int ip(String s){
return Integer.parseInt(s);
}
static class multipliers implements Comparator<Long>{
public int compare(Long a,Long b) {
if(a<b)
return 1;
else if(b<a)
return -1;
else
return 0;
}
}
static class multipliers1 implements Comparator<Coin>{
public int compare(Coin a,Coin b) {
if(a.a < b.a)
return 1;
else if(a.a > b.a)
return -1;
else{
if(a.b < b.b)
return 1;
else if(a.b > b.b)
return -1;
else
return 0;
}
}
}
// Java program to generate power set in
// lexicographic order.
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static PrintWriter w = new PrintWriter(System.out);
static class Student1
{
int id;
//int x;
int b;
//long z;
Student1(int id,int b)
{
this.id = id;
//this.x = x;
//this.s = s;
this.b = b;
// this.z = z;
}
}
} | JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
input = sys.stdin.buffer.readline
def print(val):
sys.stdout.write(str(val) + '\n')
def temp(x,t,h,c):
return abs((t*(2*x+1) - ((x+1)*h + x*c))/(2*x+1))
def prog():
for _ in range(int(input())):
h,c,t = map(int,input().split())
if t <= (h+c)/2:
print(2)
else:
R = 10**6
L = 0
while R != L:
s = (R-L)//3
m1 = L + s
m2 = R - s
if temp(m2,t,h,c) >= temp(m1,t,h,c):
R = m2 - 1
else:
L = m1+1
print(2*R + 1)
prog()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #8:47
MOD=10**9+7
INT_MAX=10**20+7
def INPUT():return list(float(i) for i in input().split())
def LIST_1D_ARRAY(n):return [0 for _ in range(n)]
def LIST_2D_ARRAY(m,n):return [[0 for _ in range(n)]for _ in range(m)]
def power(a,x):
res=1
while(x>0):
if x&1:
res=(res*a)%MOD
a=(a*a)%MOD
x//=2
return res%MOD
def NCR_MOD(n,r):
num=1
for i in range(n,n-r,-1):num=(num*i)%MOD
for j in range(1,r+1):num=(num*power(j,MOD-2))%MOD
return num
import math
#################################################################################
def F(h,c,t,y):
return abs((c+h-2*t)*y+(h-t))/(2*y+1)
def calc(h,c,t):
if h==t:
return 1
if (h+c)>=2*t:
return 2
rest=INT_MAX
q=(h-t)/(2*t-h-c)
if int(q)!=q:
f=int(q)
s=f+1
#print(F(h,c,t,f),F(h,c,t,s))
if F(h,c,t,f)<=F(h,c,t,s):
return 2*f+1
else:
return 2*s+1
else:
return int(2*q+1)
for i in range(int(input())):
h,c,t=INPUT()
print(calc(h,c,t))
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
long long int power(long long int x, long long int y,
long long int mod = 1000000007) {
long long int ret = 1;
x = x % mod;
while (y) {
if (y & 1) ret = (ret * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return ret % mod;
}
long long int curbest(long long int d, long long int x) {}
void solve() {
long long int th, tc, x;
cin >> th >> tc >> x;
long long int low = 0;
long long int high = 1e12;
auto val = [&](long long int mid) {
long double ret = 1;
ret = (long double)(mid * (tc + th) - tc) / (2 * mid - 1);
return abs(x - ret);
};
long double eqdiff = (long double)x - (long double)(tc + th) / 2;
if (2 * x == tc + th) {
cout << 2 << endl;
return;
}
if (x == th) {
cout << 1 << endl;
return;
}
while (low + 1 < high) {
long long int mid = (high - low + 1) / 2 + low;
if (val(mid) <= val(mid + 1)) {
high = mid;
} else
low = mid;
if (mid == (high - low + 1) / 2 + low) break;
}
low++;
long double bd = val(low);
long long int turn = 2 * low - 1;
long long int lturn = turn;
for (long long int i = max(1ll, lturn - 1000); i < lturn + 1000; i += 2) {
if (val(i / 2) < bd) {
bd = val(i);
turn = i;
}
}
if (bd < eqdiff) {
cout << turn << endl;
} else {
cout << 2 << endl;
}
}
signed main() {
long long int t;
cin >> t;
while (t--) solve();
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.nio.charset.Charset;
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
double h = sc.nextInt();
double c = sc.nextInt();
double d = sc.nextDouble();
double mid = (c + h) * 0.5;
int best = 1;
if (d >= h) {
best = 1;
} else if (d <= mid) {
best = 2;
} else {
//d -= mid;
int p = (int) ((h - mid) / (d-mid));
if (p % 2 == 0) p--;
//double d1 = mid + (h - mid) / p;
//double d2 = mid + (h - mid) / (p + 2);
//best = Math.abs(d1-d) <= Math.abs(d2-d) ? p : p + 2;
//System.out.println(d1 + " " + Math.abs(d1-d));
//System.out.println(d2 + " " + Math.abs(d2-d));
//System.out.println(p);
d -= mid;
double a = h - mid;
//System.out.println(a);
//System.out.println(p * (d * p + 2*d - a));
best = a <= p * (d * p + 2*d - a) ? p : p + 2;
}
System.out.println(best);
}
}
// 100 50 66 50 60 50 438837 375205 410506
// 999977 17 499998
// 48 24
// d => a / p v a / (p+2)
// d - a / p == d - a/p2
// (dp - a) (p + 2) == (dp + 2d - a) p
// -dpp - 2dp + ap + 2a ? dpp + 2dp - ap
// a = 499980 d=1 p=499979
// a ? dpp + 2dp - ap = p (dp + 2d - a)
// 499980 ? 499979 ( 499979 + 2 - 499980)
// 50 + 50 / (2k+1)
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int te;
cin >> te;
for (int q = 0; q < te; q++) {
long long h, c, t;
cin >> h >> c >> t;
if (c + h - 2 * t >= 0) {
cout << 2 << '\n';
continue;
}
long long x = (h - t) / (2 * t - h - c);
long double tb1 = double((x + 2) * h + (x + 1) * c) / (2 * (x + 1) + 1);
long double tb2 = double((x + 1) * h + x * c) / (2 * x + 1);
if (abs(t - tb1) < abs(t - tb2))
cout << (x + 1) * 2 + 1 << endl;
else
cout << x * 2 + 1 << endl;
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from fractions import Fraction
for _ in range(int(input())):
h,c,t=map(Fraction,input().split())
if 2*t<=(h+c):
print(2)
else:
x1=(h-t)//(2*t-h-c)
x2=x1+1
x1,x2=Fraction(x1),Fraction(x2)
y1,y2=Fraction(h*(x1+1)+c*x1)/Fraction(2*x1+1),Fraction(h*(x2+1)+c*x2)/Fraction(2*x2+1)
#print(y1,y2)
#print(y1,y2)
if abs(y1-t)<=abs(y2-t):
print(2*x1+1)
else:
print(2*x2+1) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | def main(h, c, t):
lowtmp = (h + c) / 2
if t == h: return 1
if t <= lowtmp: return 2
def tempSum(n):
# note, here we cannot get temp but only to get tempSum
# otherwise the tmp we get will have not enough floating precision
return (n * h + (n - 1) * c) # / (2* n -1)
l = 1
# OMG: IF 10**6 will fail for test case (7 1 6)
r = 10**7 # must be a LARGE number
while l < r:
mid = (l + r) // 2
if tempSum(mid) <= t * (2 * mid - 1):
r = mid
else:
l = mid + 1
# print('low is ', l)
# print('high is', r)
ltemp = tempSum(l)
lltemp = tempSum(l - 1)
# rtemp = temp(r)
# print('l', l, 2*l+1)
# print('r', r, 2*r +1)
# print(ltemp, rtemp)
ln = 2 * l - 1
lln = 2 * l - 3
## here as t is always an integer, we don't need to cmp lowtmp with l and l-1 any more
## l and l-1 will definitely cross the t
## # for case (7 1 6), a==b, so we should choose the smaller one, so we use >= here
## note: X first then - has better precision then - then X
# if abs((ltemp - t * ln) *ln*lln) >= abs((lltemp - t * lln)*ln*lln):
if abs(ltemp * lln - t * ln * lln) >= abs(lltemp * ln - t * ln * lln):
return 2 * (l - 1) - 1
else:
return 2 * l - 1
N = int(input())
for _ in range(N):
h, c, t = list(map(int, input().split(' ')))
print(main(h, c, t))
# h, c = 30, 10
# h, c = 41, 15
# h, c= 7, 1
# hc = [h, c]
# sm = []
# ans = []
# for i in range(200):
# if i == 0:
# ans.append(h)
# sm.append(h)
# else:
# sm.append(sm[-1] + hc[i%2])
# ans.append(sm[-1] / (i + 1))
# print(ans)
# # print(sm)
# print(len(ans))
'''
6
30 10 20
41 15 30
41 15 29
18 13 18
7 1 6
999992 10 500002
output:
2
7
13
1
1
499981
'''
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from __future__ import division
from sys import stdin
from decimal import Decimal
ceil1 = lambda a, b: (a + b - 1) // b
out = []
for _ in range(int(input())):
h, c, t = map(int, stdin.readline().split())
d1, d2 = h - t, t - c
if d1 >= d2:
out.append(2)
else:
lo = (c - t) // (h + c - 2 * t)
hi = lo + 1
tem1 = (lo * h + (lo - 1) * c)
tem2 = (hi * h + (hi - 1) * c)
d1, d2 = tem1 / Decimal(lo * 2 - 1), tem2 / Decimal(hi * 2 - 1)
# print(lo, hi, abs(d1 - t), abs(d2 - t))
if abs(t - d1) <= abs(t - d2):
out.append(lo * 2 - 1)
else:
out.append(hi * 2 - 1)
print('\n'.join(map(str, out)))
| PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | def f(h: int, c: int, t: int) -> int:
if h + c >= t * 2:
return 2
else:
x = (h - t) // (t * 2 - h - c)
lhs = (h * (x + 1) + x * c) * (x * 2 + 3) - \
t * (x * 2 + 1) * (x * 2 + 3)
rhs = t * (x * 2 + 1) * (x * 2 + 3) - \
(h * (x + 2) + (x + 1) * c) * (x * 2 + 1)
if lhs <= rhs:
return x * 2 + 1
else:
return x * 2 + 3
T = int(input())
for _ in range(T):
h, c, t = map(int, input().split())
print(f(h, c, t))
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from math import ceil ,floor
for _ in range(int(input())):
h,c,t=map(int,input().split())
if t==h:
print(1)
elif t<=(h+c)/2:
print(2)
else:
f=(h-t)/(2*t-h-c)
# print(f,abs((c*ceil(f)+(ceil(f)+1)*h)/(2*ceil(f)+1) -t ),abs((c*floor(f)+(floor(f)+1)*h)/(2*floor(f)+1)-t))
val1=((c*ceil(f)+(ceil(f)+1)*h)-t*(2*ceil(f)+1))*(2*floor(f)+1)
val2=((c*floor(f)+(floor(f)+1)*h)-t*(2*floor(f)+1))*(2*ceil(f)+1)
if abs(val1)<abs(val2):
print(2*ceil(f)+1)
else:
print(2*floor(f)+1)
# else:
# 9
# 2
# 2
# 2
# 3
# 2
# 2
# 2
# 2
# 2
# 3
# 5
# 1
# 5
# 2
# 2
# 3
# 3
# 1
# 5
# 5
# 2
# 1 | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | // Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int T =in.nextInt();
for(int i=0;i<T;i++){
int h =in.nextInt();int c =in.nextInt();int t =in.nextInt();
Solution s=new Solution();
s.solution(h,c,t);
}
}
}
class Solution{
public void solution(int h,int c,int T){
double t=T;
double avg=(h+0.0+c)/2;
if(t<=avg){
System.out.println(2);
return;
}
if(t>=h){
System.out.println(1);
return;
}
long pos=-1;
long l=2,r=1000000;
double sum=h+c;
while(l<=r){
long mid=l+(r-l)/2;
double ave=((mid-1)*(sum)+h+0.0)/(mid*2-1);
if(t>=ave){
pos=mid;
r=mid-1;
}else{
l=mid+1;
}
}
//consider 2 case
double p1=pos,p2=pos-1,p3=pos+1;
PriorityQueue<BigDecimal[]>pq=new PriorityQueue<>((a1,a2)->{
int val=a1[1].compareTo(a2[1]);
if(val==0){
return a1[0].compareTo(a2[0]);
}
else return val;
});
//System.out.println(p1+" "+Math.abs(t-((p1-1)*(sum)+h+0.0)/(p1*2-1)));
//System.out.println(p2+" "+Math.abs(t-((p2-1)*(sum)+h+0.0)/(p2*2-1)));
//System.out.println(p3+" "+Math.abs(t-((p3-1)*(sum)+h+0.0)/(p3*2-1)));
pq.add(new BigDecimal[]{new BigDecimal(p1),( new BigDecimal(t).subtract( (new BigDecimal(p1-1).multiply(new BigDecimal(sum))).add(new BigDecimal(h)).divide(new BigDecimal(p1*2-1),20,RoundingMode.CEILING)) ).abs()});
pq.add(new BigDecimal[]{new BigDecimal(p2),(new BigDecimal(t).subtract( (new BigDecimal(p2-1).multiply(new BigDecimal(sum))).add(new BigDecimal(h)).divide(new BigDecimal(p2*2-1),20,RoundingMode.CEILING)) ).abs()});
//pq.add(new BigDecimal[]{new BigDecimal(p3),(new BigDecimal(t).subtract( (new BigDecimal(p3-1).multiply(new BigDecimal(sum))).add(new BigDecimal(h)).divide(new BigDecimal(p3*2-1),RoundingMode.CEILING)) ).abs()});
System.out.println((int)(pq.peek()[0].doubleValue()*2-1));
}
} | JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
input = sys.stdin.readline
def print(val):
sys.stdout.write(str(val) + '\n')
def val(x,h,c):
return (h-c)/(2*(x*2 -1))
def prog():
for _ in range(int(input())):
L = 1
R = 10**12
h,c,t = map(int,input().split())
if t <= (h+c)/2:
print(2)
else:
t -= (h+c)/2
while R > L:
m = (R+L)//2
temp = val(m,h,c)
if temp > t:
L = m+1
else:
R = m-1
left = val(L,h,c)
if left > t:
right = val(L+1,h,c)
if abs(t-left) <= abs(t-right):
print(2*L-1)
else:
print(2*L+1)
else:
right = val(L-1,h,c)
if abs(t-left) >= abs(t-right):
print(2*L-3)
else:
print(2*L-1)
prog()
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import math
for _ in range(int(input())):
h, c, t = map(int, input().split())
if 2 * t <= h + c:
print(2)
continue
if t >= h:
print(1)
continue
n = int((h - t) / (2 * t - h - c))
t1 = abs(((h + c) * n + h) - t * (2 * n + 1)) * (2 * n + 3)
t2 = abs(((h + c) * (n + 1) + h) - t * (2 * n + 3)) * (2 * n + 1)
if t1 <= t2: print(2 * n + 1)
else: print(2 * n + 3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.*;
import java.math.*;
import java.util.*;
public class Main{
static long MOD = 1000000007L;
static long [] fac;
static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static long lMax = 0x3f3f3f3f3f3f3f3fL;
static int iMax = 0x3f3f3f3f;
static HashMap <Long, Long> memo = new HashMap();
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// Start writing your solution here. -------------------------------------
int t = sc.nextInt();
//int t = 1;
while(t-- > 0) {
int hh = sc.nextInt();
int cc = sc.nextInt();
int mm = sc.nextInt();
int res = 0;
if(mm == hh) res = 1;
else if(hh + cc >= 2 * mm) res = 2;
else
{
double n = 0.5 * hh - 0.5 * cc;
double m = 0.5 * hh + 0.5 * cc;
double x = 1.0 * mm - m;
int k = (int)(n / x);
double min = 1000000000.0;
for(int i = k - 5; i <= k + 5; i++) {
if (i % 2 == 0) continue;
double tmp = Math.abs(n / i - x);
if(tmp < min)
{
min = tmp;
res = i;
}
}
}
out.println(res);
}
out.close();
}
public static long C(int n, int m)
{
if(m == 0 || m == n) return 1l;
if(m > n || m < 0) return 0l;
long res = fac[n] * quickPOW((fac[m] * fac[n - m]) % MOD, MOD - 2) % MOD;
return res;
}
public static long quickPOW(long n, long m)
{
long ans = 1l;
while(m > 0)
{
if(m % 2 == 1)
ans = (ans * n) % MOD;
n = (n * n) % MOD;
m >>= 1;
}
return ans;
}
public static int gcd(int a, int b)
{
if(a % b == 0) return b;
return gcd(b, a % b);
}
public static long gcd(long a, long b)
{
if(a % b == 0) return b;
return gcd(b, a % b);
}
public static long solve(long cur){
return 0L;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.StringTokenizer;
/*
1
50 10 49
2
99999 0 50000
99999 0 50002
*/
public class C2 {
static double EPS=1e-8;
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
long hot=fs.nextInt(), cold=fs.nextInt(), target=fs.nextInt();
target-=cold;
hot-=cold;
if (2*target<=hot) {
System.out.println(2);
continue;
}
long ans=target/(2*target-hot);
long nTurns1=ans;
// System.out.println(nTurns1);
long nTurns2=nTurns1+1;
// System.out.println(error1+" "+error2+" "+eval(nTurns1-1, target));
long n1=hot*(nTurns1)-target*(nTurns1+nTurns1-1);
long d1=nTurns1+nTurns1-1;
long n2=hot*(nTurns2)-target*(nTurns2+nTurns2-1);
long d2=nTurns2+nTurns2-1;
// System.out.println(n1+" "+n2);
n2*=-1;
if (lessThan(n2, d2, n1, d1)) {
System.out.println(nTurns2*2-1);
}
else {
System.out.println(nTurns1*2-1);
}
}
out.close();
}
// returns true if a/b < x/y
// runs in log time without overflowing longs
// only works on NON-NEGATIVE numbers
static boolean lessThan(long a, long b, long x, long y) {
if (a / b != x / y)
return a / b < x / y;
if (x % y == 0) return false;
if (a % b == 0) return true;
return lessThan(y, x % y, b, a % b);
}
// use this if the fractions can be negative
static boolean lessThanWithSigns(long a, long b, long x, long y) {
int sign1 = Long.signum(a) * Long.signum(b);
int sign2 = Long.signum(x) * Long.signum(y);
if (sign1 != sign2) return sign1 < sign2;
a = Math.abs(a); b = Math.abs(b);
x = Math.abs(x); y = Math.abs(y);
if (sign1 < 0)
return lessThan(x, y, a, b);
else
return lessThan(a, b, x, y);
}
static double eval(long n, double target) {
double value=n/((double)n+(n-1));
return Math.abs(value-target);
}
static boolean eq(double a, double b) {
return Math.abs(a-b)<EPS;
}
static boolean leq(double a, double b) {
return a-EPS<b;
}
static boolean geq(double a, double b) {
return a+EPS>b;
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long h, c, t;
cin >> h >> c >> t;
if ((h + c) / 2 >= t) {
cout << 2 << '\n';
return;
}
long long a = t - h;
long long b = h + c - 2 * t;
long long k = 2 * (a / b) + 1;
long long val2 =
abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h - t * 1ll * (k + 2));
long long val1 = abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k);
if (val1 * (k + 2) <= val2 * k) {
cout << k << '\n';
} else
cout << k + 2 << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) solve();
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | // Java implementation of the approach
import java.io.*;
import java.util.*;
public class Solution {
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static FastReader sc = new FastReader();
static List<Integer>[] edges;
public static int[][] parent;
public static int col = 20;
public static void main(String[] args) throws IOException {
int tc = sc.nextInt();
while(tc-->0) {
int h = sc.nextInt();
int c = sc.nextInt();
int t = sc.nextInt();
if(h == t) out.println(1);
else if(h + c >= 2 * t) out.println(2);
else if(h + c < 2 * t){
long x=(h-t)/(2*t-h-c);
double abs1=Math.abs(t*(2*x+1)-((x+1)*h+x*c))/(2*x+1.0);
x++;
double abs2=Math.abs(t*(2*x+1)-((x+1)*h+x*c))/(2*x+1.0);
if(abs2>=abs1)
x--;
out.println(2*x+1);
}
}
out.close();
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | a=int(input())
from fractions import Fraction
import sys
input=sys.stdin.readline
for i in range(a):
h,c,t=map(int,input().split())
se=(h+c)/2
if(h==c):
print(1)
elif(t<=se):
print(2)
elif(t>se):
x=h-t
y=2*(t-se)
if(x%y==0):
z=int(x//y)
print(2*z+1)
continue;
else:
z=int((x//y))+1
num1=abs(z*c+(z+1)*h)
dum1=abs(2*z+1)
if(z>=0):
z-=1
num2=abs((z*c+(z+1)*h))
dum2=abs(2*z+1)
if(t*dum2>num2):
if(num2*dum1<=dum2*num1):
print(2*z+1)
else:
print(2*z+3)
elif(t*dum2==num2):
print(2*z+1)
else:
if(num2*dum1+num1*dum2<=2*t*(dum2*dum1)):
print(2*z+1)
else:
print(2*z+3)
else:
print(2*z+1)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
import math
import collections
from sys import stdin,stdout,setrecursionlimit
import bisect as bs
setrecursionlimit(2**20)
M = 10**9+7
T = int(stdin.readline())
# T = 1
def ceil(x,y):
return x//y + (1 if x%y != 0 else 0)
def fun(x,h,c,t):
return abs((x*c + (x+1)*h)/(2*x+1)-t)
for _ in range(T):
# n = int(stdin.readline())
h,c,t = list(map(int,stdin.readline().split()))
# d = list(map(int,stdin.readline().split()))
# q = list(map(int,stdin.readline().split()))
# b = list(map(int,stdin.readline().split()))
# s = stdin.readline().strip('\n')
if(h == t):
print(1)
continue
if(t <= (h+c)/2):
print(2)
continue
a = []
x = ceil(h-t, 2*t-c-h)
y = (h-t)//(2*t-c-h)
num1 = y*c + (y+1)*h - (2*y+1)*t
den1 = 2*y + 1
num2 = (2*x+1)*t - x*c - (x+1)*h
den2 = (2*x + 1)
# print(x,y)
# print(num1,den1,num2,den2)
if(num1*den2 - num2*den1 > 0):
print(int(2*x+1))
else:
print(int(2*y+1))
# a.append([abs(((c+h)/2)-t),2])
# a.append([fun(x,h,c,t), 2*x+1])
# a.append([fun(y,h,c,t), 2*y+1])
# ans = a[0][1]
# mn = a[0][0]
# print(a)
# for diff, num in a[1:]:
# # print(diff,num,ans)
# if(diff < mn):
# mn = diff
# ans = num
# if(diff == mn):
# # print('*')
# if(ans >= num):
# ans = num
# # print(mn,ans)
# print(int(ans)) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sys,math
from collections import deque
#input = sys.stdin.buffer.readline
def solve():
return;
for _ in range(int(input())):
h,c,t = map(int,input().split())
if t<=(h+c)/2:
print(2)
else:
x = (h-t)//(2*t-h-c)
v1 = ((x+1)*h + x*c)
v2 = ((x+2)*h + (x+1)*c)
# print(v1,v2)
# print(abs(v1-(2*x+1)*t)/(2*x+1),abs(t*(2*x+3)-v2)/(2*x+3))
if abs(v1-(2*x+1)*t)/(2*x+1)<=abs(t*(2*x+3)-v2)/(2*x+3):
print(2*x+1)
else:
print(2*x+3)
| PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | for _ in range(int(input())):
h,c,t = map(int,input().split())
if(h==t):print(1)
else:
xx=(h+c)/2
if(xx>=t): print(2)
else:
dif=(t-xx)
j=int(abs((c-h)//(2*dif)))
if(j%2==0):
j+=1
if(dif-(h-c)/(2*j)>=abs(dif-(h-c)/(2*(j-2)))):
print(j-2)
else:
print(j) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from fractions import *
T = input()
def calc(h, total, Tmult):
mult = Tmult*2+1
return Fraction(h)/Fraction(mult) + Fraction(Tmult)*Fraction(total)/Fraction(mult)
for _ in xrange(T):
h, c, t = map(float, raw_input().split())
total = h+c
if t == h:
print 1
elif total%2 == 0 and t == total // 2:
print 2
elif t < total / 2:
print 2
else:
now_orig = Fraction(1) / ((Fraction(h)-Fraction(c)) / (Fraction(h)-Fraction(t)) - Fraction(2))
now = int(now_orig)
nd = calc(h, total, now)
ndd = calc(h, total, now+1)
#print now_orig
#print now, nd
#print now+1, ndd
#print abs(Fraction(nd) - Fraction(t)), abs(Fraction(ndd) - Fraction(t))
#print abs(Fraction(nd) - Fraction(t)) > abs(Fraction(ndd) - Fraction(t)), abs(Fraction(nd) - Fraction(t)) < abs(Fraction(ndd) - Fraction(t))
if abs(Fraction(nd) - Fraction(t)) <= abs(Fraction(ndd) - Fraction(t)):
print 2*now+1
else:
print 2*now + 3 | PYTHON |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import java.util.Scanner;
public class C1359 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
StringBuilder output = new StringBuilder();
for (int t=0; t<T; t++) {
int H = in.nextInt();
int C = in.nextInt();
int temp = in.nextInt();
int answer;
if (temp >= H) {
answer = 1;
} else if (temp <= (C+H)/2d) {
answer = 2;
} else {
double n = (H-C)/(4d*(temp-(H+C)/2d))-1/2;
long nup = Math.max(0, (long) Math.ceil(n));
long ndown = Math.max(0, (long) Math.floor(n));
double tempup = ((nup+1)*H+nup*C)/(2d*nup+1);
double tempdown = ((ndown+1)*H+ndown*C)/(2d*ndown+1);
double diffup = Math.abs(tempup-temp);
double diffdown = Math.abs(tempdown-temp);
long nn = (diffdown <= diffup) ? ndown : nup;
answer = (int) (2*nn + 1);
}
output.append(answer).append('\n');
}
System.out.print(output);
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | import sun.nio.cs.ext.MacThai;
import java.io.*;
import java.util.*;
public class Codeforces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
private static int[] rai(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
private static int[][] rai(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextInt();
}
}
return arr;
}
private static long[] ral(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
return arr;
}
private static long[][] ral(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextLong();
}
}
return arr;
}
private static int ri() {
return s.nextInt();
}
private static long rl() {
return s.nextLong();
}
private static String rs() {
return s.next();
}
public static void main(String[] args) {
StringBuilder ans = new StringBuilder();
int t = ri();
while (t-- > 0) {
long h=ri();
long c=ri();
long temp=ri();
if(h==temp)
{
ans.append("1").append("\n");
continue;
}
if((h+c)>=2*temp)
{
ans.append("2").append("\n");
continue;
}
long k1= (long) ((h-temp)/(2*temp-h-c));
// System.out.println(k1);
long[] f1={(k1+1)*h+k1*c,2*k1+1};
long[] f2={(k1+2)*h+(k1+1)*c,2*k1+3};
long[] st={f1[0]*f2[1]+f1[1]*f2[0],f1[1]*f2[1]};
// System.out.println(st[0]+" "+2*temp*st[1]);
if(st[0]<=2*temp*st[1])
{
ans.append(2*k1+1);
}
else
{
ans.append(2*k1+3);
}
ans.append("\n");
}
out.print(ans.toString());
out.flush();
}
}
| JAVA |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int cas;
cin >> cas;
while (cas--) {
int h, t, c;
cin >> h >> c >> t;
h -= c;
t -= c;
if (h >= 2 * t) {
cout << 2 << endl;
} else {
LL n = (h - t) / (2 * t - h);
auto f = [](LL n, int h, int t) {
return (n + 1) * (2 * n + 3) * h + (2 * n + 1) * (n + 2) * h >
(2 * n + 1) * (2 * n + 3) * t * 2;
};
if (f(n, h, t)) ++n;
cout << 2 * n + 1 << endl;
}
}
return 0;
}
| CPP |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | from fractions import Fraction as f
def answer():
h, c, t = list(map(int, input().split()))
if t <= (h+c)/2:
return 2
else:
if abs(t - h) <= abs(t - (h*2 + c)/3):
return 1
n = int((t - h) / (h + c - 2*t))
i = max(2,n-2)
while abs(t -f( h*i + c*(i-1) , i*2-1)) > abs(t - f(h*(i+1) + c*(i) , (i+1)*2-1) ):
i += 1
return int(i*2) - 1
for _ in range(int(input())):
print(answer()) | PYTHON3 |
1359_C. Mixing Water | There are two infinite sources of water:
* hot water of temperature h;
* cold water of temperature c (c < h).
You perform the following procedure of alternating moves:
1. take one cup of the hot water and pour it into an infinitely deep barrel;
2. take one cup of the cold water and pour it into an infinitely deep barrel;
3. take one cup of the hot water ...
4. and so on ...
Note that you always start with the cup of hot water.
The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.
You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is t_b, then the absolute difference of t_b and t (|t_b - t|) should be as small as possible.
How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
Input
The first line contains a single integer T (1 β€ T β€ 3 β
10^4) β the number of testcases.
Each of the next T lines contains three integers h, c and t (1 β€ c < h β€ 10^6; c β€ t β€ h) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
Output
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.
Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.
In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won't get us closer to t than that.
In the third testcase the temperature after 1 poured cup: 1 hot is 18. That's exactly equal to t. | 2 | 9 | t=int(input())
for _ in range(t):
h,c,t=map(int,input().split(" "))
tb = (h+c)//2
if t==h:
print(1)
else:
if t<=tb:
print(2)
else:
k=(t-h)//(h+c-2*t)
if abs((k*(h+c)+h)-t*(2*k+1))*(2*k+3)<=abs(((k+1)*(h+c)+h)-t*(2*k+3))*(2*k+1):
print(2*k+1)
else:
print(2*(k+1)+1) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.