Suppose we partition the elements of an array π into any number π of non-empty multisets π1,π2,β¦,ππ, where π is an arbitrary positive integer. Define the score of π as the maximum value of MEX(π1)β+MEX(π2)+β¦+MEX(ππ) over all possible partitions of π for any integer π.
Envy is given an array π of size π. Since he knows that calculating the score of π is too easy for you, he instead asks you to calculate the sum of scores of all 2πβ1 non-empty subsequences of π.β Since this answer may be large, please output it modulo 998244353.
βMEX of a collection of integers π1,π2,β¦,ππ is defined as the smallest non-negative integer π₯ that does not occur in the collection π. For example, MEX([0,1,2,2])=3 and MEX([1,2,2])=0
β A sequence π₯ is a subsequence of a sequence π¦ if π₯ can be obtained from π¦ by deleting several (possibly, zero or all) elements.
Input
The first line contains an integer π‘ (1β€π‘β€104) β the number of test cases.
The first line of each test case contains an integer π (1β€πβ€2β 105) β the length of π.
The second line of each test case contains π integers π1,π2,β¦,ππ (0β€ππ<π) β the elements of the array π.
It is guaranteed that the sum of π over all test cases does not exceed 2β 105.
Output
For each test case, output the answer, modulo 998244353.
Example
inputCopy
4
3
0 0 1
4
0 0 1 1
5
0 0 1 2 2
4
1 1 1 1
outputCopy
11
26
53
0
Note
In the first testcase, we must consider seven subsequences:
[0]: The score is 1.
[0]: The score is 1.
[1]: The score is 0.
[0,0]: The score is 2.
[0,1]: The score is 2.
[0,1]: The score is 2.
[0,0,1]: The score is 3.
The answer for the first testcase is 1+1+2+2+2+3=11.
In the last testcase, all subsequences have a score of 0. Please solve using the following template: #include
#include
#include
using namespace std;
typedef long long ll;
const int MOD = 998244353;
const int MAX_N = 200005;
void solve() {
int n; cin >> n;
vector a(n); for(auto &x : a) cin >> x;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t; cin >> t;
while (t--) solve();
}
Assistant
To solve this problem, we need to compute the sum of scores for all non-empty subsequences of the array ( a ). The score of a subsequence ( b ) is defined as the maximum sum of MEX (minimum excluded value) over all possible partitions of ( b ) into any number of non-empty multisets.
Step-by-Step Reasoning
Understanding MEX:
The MEX of a multiset ( S ) is the smallest non-negative integer not present in ( S ).
For example, MEX([0, 1, 2]) = 3, and MEX([1, 2, 2]) = 0.
Score Calculation:
For a given subsequence ( b ), partition the elements into multisets such that the sum of their MEX values is maximized.
The score of ( b ) is determined by iteratively computing the minimum count of elements required to achieve the maximum MEX over all possible partitions.
Summing Over All Subsequences:
To compute the total score across all subsequences, the problem is transformed into a combinatorial problem where we need to calculate how the MEX values contribute across all possible partitions.
This requires efficient handling of combinations and modular arithmetic due to the potential size of the input.
Dynamic Programming Approach:
Utilize dynamic programming to handle the various ways elements can be partitioned and their contributions to the MEX values.
Precompute factorials, inverse factorials, and powers of 2 to facilitate quick calculations of combinations and subsets.
Implementation:
The solution involves iterating through possible values of MEX and efficiently updating the possible partitions using precomputed combinatorial values.
Special care is taken to handle the modulo ( 998244353 ) to prevent overflow and ensure correctness.
Final Implementation
The final code leverages precomputation and dynamic programming to efficiently compute the required sum for each test case.
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;
const int MAX = 200005;
// Fast exponentiation
ll power_pow(ll a, ll b){
ll res = 1;
a %= MOD;
while(b > 0){
if(b & 1LL){
res = res * a % MOD;
}
a = a * a % MOD;
b >>=1LL;
}
return res;
}
// Precompute factorial and inv factorial
ll fact[MAX], inv_fact[MAX];
ll inv2[MAX], power2[MAX];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Precompute factorial and inv factorial
fact[0] =1;
for(int i=1;i<MAX;i++) fact[i] = fact[i-1]*i%MOD;
inv_fact[MAX-1] = power_pow(fact[MAX-1], MOD-2);
for(int i=MAX-2;i>=0;i--) inv_fact[i] = inv_fact[i+1]*(i+1)%MOD;
// Precompute power of 2
power2[0]=1;
for(int i=1;i<MAX;i++) power2[i] = power2[i-1]*2%MOD;
// Precompute inv2
inv2[0] = 1; // not used
inv2[1] = power_pow(2, MOD-2);
for(int i=2;i<MAX;i++) inv2[i] = power_pow(2, i*(MOD-2)% (MOD-1)) ; // Not used, handled differently
int t; cin>>t;
while(t--){
int n; cin>>n;
vector<int> a(n);
for(auto &x:a) cin>>x;
// Count frequencies
vector<int> c(n,0);
for(auto x:a){
if(x < n) c[x]++;
}
// Find max_c
int max_c =0;
for(auto x:c) max_c = max(max_c, x);
// Precompute S[x][k} } up to k=max_c
// To avoid O(n^2), we precompute for each x, S[x] as a list
// S[x][k} = sum_{u=0}^{k-1} C(c[x},u}
// We can store up to k = max_c+1
// To make it efficient, use binary exponentiation for sum C(c[x},u}
// Using DP, for each x, compute prefix sums
// Precompute all necessary binomial coefficients
// We need to compute C(c[x}, u} for u=0 to min(c[x}, max_c}
// To save memory, compute on the fly
// Precompute 2^{c[x}}
vector<ll> two_c(n, 1);
for(int x=0;x<n;x++) two_c[x] = power2[c[x]];
// Precompute prefix sums for binomial coefficients
// S[x][k} } = sum_{u=0}^{k-1} C(c[x},u}
// We can compute S[x][k} } for k=1 to c[x}+1}
// Store in a vector of vectors
// But n=2e5 and k=2e5 is too much, so use another approach
// Instead, for each x, precompute up to k where k <=c[x} +1}
// We'll compute on the fly
// Now, find s[k} } for each k
// s[k} } is the first x where c[x} < k
vector<int> s(max_c+2, n+1);
int ptr =0;
for(int k=1;k<=max_c+1;k++){
while(ptr <n && c[ptr] >=k){
ptr++;
}
s[k] = ptr+1;
}
// Now, for each k, compute the sum of prefix_products up to s[k}-1}
// To handle S[x][k} }, we need to compute sum C(c[x}, u} ) for u=0 tok-1}
// Use cumulative sum
// Precompute for each x, the cumulative sums up to k-1
// To do it efficiently, precompute sum_C[x][k} }
// We'll use memoization
// Create a vector of vectors is too slow, so use a separate array and reset each time
// We will compute S[x}][k} } on the fly for each k
// Precompute all C(c[x}, u} )
// But since we need sum up to k-1, we can compute it iteratively
// Implement for each k, maintain a temporary variable
// Initialize a vector to store current sum for each x
// Initialize all to 0
// And incrementally add C(c[x},k-1} )
// Thus, we need to track for each x, the sum up to k-1
// Initialize a vector
vector<ll> sum_C(n, 0);
// Now, iterate k from1 to max_c+1
// and update sum_C[x} ] by adding C(c[x},k-1} )
// Precompute all necessary binomial coefficients on the fly
ll score_sum =0;
// Precompute inverse of 2^c[x} }
// Since 2^c[x} } can be up to 2^200k, we compute their inverses
// Compute inv_2_c[x} } = power_pow(2, MOD-2 - c[x} } )
// However, it's more straightforward to compute (2^{c[x}} - sum_C[x} ]) * inv(2^{c[x}})
// which is equivalent to (2^{c[x}} - sum_C[x} ]) * power_pow(2, c[x}} )^{MOD-2}
// We can compute it directly
// Precompute 2^{c[x}} for all x
// Precompute 2^{-c[x}}} as power_pow(2, MOD-2)^c[x} } which is power_pow(2, c[x} }*(MOD-2))
// But compute (2^{c[x}} - sum_C[x} ]) * inv(2^{c[x}} ) = 1 - sum_C[x} ] * inv(2^{c[x}} )
// which is equivalent to sum_{u=k}^c[x} C(c[x},u} ) / 2^{c[x}} }
// which we can compute as follows:
// To avoid precision issues, compute (2^{c[x}} - sum_C[x} ]) * inv(2^{c[x}} )
// where sum_C[x} ] is already precomputed.
// Now iterate k
for(int k=1;k<=max_c+1;k++){
// Update sum_C[x} ] by adding C(c[x},k-1} )
// Precompute C(c[x},k-1} )
// We can compute it directly using fact and inv_fact
for(int x=0;x<n;x++) {
if(k-1 <=c[x} }){
ll C_val = (fact[c[x} ] * inv_fact[k-1] % MOD) * inv_fact[c[x} ] - (k-1)>=0? c[x}-k+1:0) % MOD;
// Correct binomial coefficient
// It's better to use the combinatorial formula
// If k-1 >c[x}, then C=0
ll C_correct = 0;
if(k-1 <=c[x} ]) C_correct = fact[c[x}] * inv_fact[k-1] % MOD * inv_fact[c[x}] - (k-1) % MOD + MOD) % MOD;
sum_C[x} ] = (sum_C[x} ] + C_correct) % MOD;
}
}
// Now, compute P[x} >=k} } for each x
// and iterate m from1} to s[k} }
int sk = s[k};
// Precompute P[x} >=k} } for x=0} to sk-1}
// which is (2^{c[x}} - sum_C[x} ]) * inv(2^{c[x}} ) % MOD
// which is (2^{c[x} } - sum_C[x} ]) * power_pow(2, c[x} }, MOD-2)
// But power_pow(2, c[x} }, MOD-2) = power2[c[x} ]^{MOD-2} which is not directly computable
// Instead, compute (2^{c[x}} - sum_C[x} ]) * inv(2^{c[x} } ) as:
// (2^{c[x}} - sum_C) * power_pow(2, c[x}} )^{-1} % MOD
// Precompute inv(2^{c[x}} )
// Which is power_pow(2, c[x}} +(MOD-2))
// Thus, precompute inv_2c[x} }
// To compute inv_2c[x}, precompute all powers up to MAX
// Here, compute inv_2c[x} }
// Note that inv(2) = power_pow(2, MOD-2)
// So inv(2^{c[x}} ) = power_pow(inv(2), c[x}} )
// Precompute inv2 once
// Let inv2_val = power_pow(2, MOD-2)
ll inv2_val = power_pow(2, MOD-2);
ll inv_2c = 1;
// Compute inv(2^{c[x} } ) = inv2^{c[x} }
// which is power_pow(inv2_val, c[x} )
// But since inv2_val = power_pow(2, MOD-2),
// inv_2c = power_pow(2, (ll)(MOD-2)*c[x} ) % MOD
// which is equivalent to power_pow(2, MOD-2-c[x} }*(MOD-2)/?) Not feasible
// Instead, compute inv(2^{c[x} } ) as power_pow(2, MOD-2-c[x} ) if possible
// But it's better to compute it iteratively
// Precompute inv2 up to MAX
// Instead, assume c[x} ] <=200k, which is manageable
// Precompute powers of inv2_val
// compute inv_2c for each x up to c[x} }
// Precompute inv2_pows
// which is power_pow(2, MOD-2) raised to c[x} }
// which is power_pow(inv2_val, c[x} } )
// but implement it as a separate power
// Thus, for each x,
// inv_2c[x} ] = power_pow(2, MOD-2) raised to c[x} }, which is power_pow(2, (ll)(MOD-2)*c[x} ) % MOD
// Implement as:
vector<ll> P_ge_k(sk-1 >=0? sk-1 :0, 1);
for(int x=0;x<sk && x<n;x++){
if(x >=n) break;
if(c[x} ] <k) break;
// Compute (2^{c[x} ] - sum_C[x} ]) * inv(2^{c[x} } )
// = (power2[c[x} ] - sum_C[x} ]) * power_pow(power2[c[x} ], MOD-2) % MOD
ll numerator = (power2[c[x} ] - sum_C[x} ] + MOD) % MOD;
ll denom = power_pow(power2[c[x} ], MOD-2);
ll P = numerator * denom % MOD;
P_ge_k[x} ] = P;
}
// Now, compute the sum of prefix products
ll prefix_prod =1;
ll sum_pref =0;
for(int m=0;m<sk-1 && m<n;m++){
sum_pref = (sum_pref + prefix_prod) % MOD;
prefix_prod = prefix_prod * P_ge_k[m} ] % MOD;
}
// If sk-1 ==n, need to include the last m
if(sk-1 <n){
sum_pref = (sum_pref + prefix_prod) % MOD;
}
// Now, multiply by 2^{total} = power2[n}
ll contrib = sum_pref * power2[n] % MOD;
score_sum = (score_sum + contrib) % MOD;
}
// Now, subtract the empty subset
// Because the problem asks for non-empty subsequences
// The empty subset is not included, but our sum includes all subsets including empty
// However, score(empty) is undefined or 0
// So no need to subtract
// But we computed 2^{n} * something, which already excludes empty subset in the formula
// Thus, output the score_sum
cout<<score_sum%MOD<<"\n";
}
}