Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Only the frequencies matter. If value appears times in the original multiset, the output just chooses some count with .
Fix the set of values that appear at least once in the output. For each , there are exactly choices for : .
The real question is: when is a chosen set possible at all? Let . Any value with frequency must fit into groups whose chosen modes have enough total “mode copies” to absorb it.
If the output only uses values from , the total number of copies available to serve as chosen-mode copies is at most . Necessarily this must be at least .
That condition is also sufficient. So count all subsets with , weighted by . Compute it as total minus a knapsack DP over subset sums .
Let be the frequency of value in the original multiset. Values with are useless, so ignore them.
We need count possible output frequency vectors , where is how many times value appears in the generated multiset .
Obviously,
because each group that outputs must contain at least one copy of .
The non-obvious part is deciding which combinations of positive are actually possible.
The Key Characterization
Let
be the largest original frequency.
For some candidate output, define
So is the set of values that appear in the output at least once.
The claim is:
That is the whole problem. The rest is just counting.
Why This Condition Is Necessary
Look at any valid partition. Each group has a chosen mode. Suppose a group chooses value , and appears times inside that group.
Then every other value can appear at most times in that group, otherwise would not be a mode.
Now add up these chosen-mode counts over all groups. Call the total .
Take any value . Across all groups, the copies of can never exceed this total capacity : in groups choosing , its own copies contribute to ; in groups not choosing , it can appear at most as many times as the chosen mode of that group.
So every frequency satisfies
In particular,
But the chosen modes only come from values in , so the total number of chosen-mode copies cannot exceed all copies of values in :
Therefore,
No way around it. If the biggest pile has size , your selected output values need enough total mass to contain/absorb it. Math does not care about vibes.
Why This Condition Is Sufficient
Now suppose we picked some set such that
Also suppose for each we picked any positive count
We will build a valid partition.
For every selected value , split all copies of among exactly groups, all choosing as their mode. This is always possible because .
Now the total chosen-mode capacity across all these groups is
Every unselected value has
So we can distribute its copies among the existing groups, never putting more copies into a group than that group's chosen-mode count. This keeps the chosen value tied for maximum or strictly maximum, so it remains a valid mode.
Different unselected values do not interfere with each other, because the mode condition compares each value's count separately. A group can contain many foreign values, each up to the chosen-mode count. Ties are allowed. Very convenient, almost suspiciously so.
Thus the condition is sufficient.
Counting Outputs
For each selected set , how many output multisets use exactly those values?
For every , can be any number from to , giving choices.
So this set contributes
if
Therefore the answer is
Instead of summing good subsets directly, count all subsets and subtract bad ones.
All subsets weighted by contribute:
because for each value we either do not select it, contributing , or select it, contributing .
Bad subsets are those with total frequency less than .
Use knapsack DP:
Initialize . For each frequency , update downward:
Only keep sums , since those are the bad ones.
Then
and
The empty subset is included in , which is exactly what we want, since the empty output is impossible.
Complexity
For each test case, the DP size is , and the number of positive frequencies is at most .
So the complexity is
and since the sum of over all tests is at most , this easily fits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
vector<int> freq;
int mx = 0;
ll total = 1;
for (int x = 1; x <= n; x++) {
if (cnt[x] == 0) continue;
freq.push_back(cnt[x]);
mx = max(mx, cnt[x]);
total = total * (cnt[x] + 1) % MOD;
}
vector<ll> dp(mx, 0);
dp[0] = 1;
for (int c : freq) {
for (int s = mx - 1 - c; s >= 0; s--) {
dp[s + c] = (dp[s + c] + dp[s] * c) % MOD;
}
}
ll bad = 0;
for (ll x : dp) bad = (bad + x) % MOD;
ll ans = (total - bad + MOD) % MOD;
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
vector<int> freq;
int mx = 0;
ll total = 1;
for (int x = 1; x <= n; x++) {
if (cnt[x] == 0) continue;
freq.push_back(cnt[x]);
mx = max(mx, cnt[x]);
total = total * (cnt[x] + 1) % MOD;
}
vector<ll> dp(mx, 0);
dp[0] = 1;
for (int c : freq) {
for (int s = mx - 1 - c; s >= 0; s--) {
dp[s + c] = (dp[s + c] + dp[s] * c) % MOD;
}
}
ll bad = 0;
for (ll x : dp) bad = (bad + x) % MOD;
ll ans = (total - bad + MOD) % MOD;
cout << ans << '\n';
}
}