题解:CF2165B Marble Council
设最终的多重集合为
假设我们已经考虑了
- 向
S 添加x 。 - 不向
S 中添加x 。
显然,第一种选择总是可行的,因为我们可以在拆分
考虑不加入某个
设值为
因此,若已经向
现在,考虑不加入所有的
假设所有的
考虑某个多重集合中被选入
因此,对于某个被选入多重集合
因此,对于一个
因此,一个最终集合
设
因此,我们设
考虑之前所有的
初始化
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int const N = 5007;
int const P = 998244353;
int n;
int a[N],cnt[N];
LL dp[N];
void solve(){
cin >> n;
for(int i=1 ;i<=n ;i++)
cin >> a[i];
for(int i=1 ;i<=n ;i++) cnt[i]=0;
for(int i=1 ;i<=n ;i++) cnt[a[i]]++;
int max_cnt = 0;
for(int i=1 ;i<=n ;i++)
max_cnt = max(max_cnt ,cnt[i]);
dp[0]=1;
for(int i=1 ;i<=n ;i++) dp[i]=0;
for(int i=1 ;i<=n ;i++) if(cnt[i]){
for(int j=n ;j>=cnt[i] ;j--) {
dp[j] = (dp[j] + 1LL*cnt[i]*dp[j-cnt[i]]%P )%P;
}
}
LL res = 0;
for(int i=max_cnt ;i<=n ;i++)
res = (res+dp[i])%P;
cout << res << "\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
while(T--) solve();
}