题解 环
赛时觉得连续段这样的东西是不太能 DP 的,遂爆零
首先将左部点重排为 \(a_i\) 单调增
令 \(f_{i, j}\) 为考虑了前 \(i\) 个左部点,右部最终选入环中的点形成了 \(j\) 个连续段方案数(可以为单点)
那么转移考虑向右部加入 \(a_{i}-a_{i-1}\) 中的一些点
然后考虑合并一些连续段
发现这样会把大小为 2 的环也算进去,那么最后减掉就好了
为什么没人写组合数啊
复杂度 \(O(n^2)\)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 5010
#define pb push_back
#define ll long long
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n;
int a[N];
const ll mod=998244353, inv2=(mod+1)>>1;
inline void md(int& a, int b) {a+=b; a=a>=mod?a-mod:a;}
namespace force{
vector<int> e[N];
int f[1<<21][21], ans;
int lid[N], rid[N], tot;
void solve() {
int lim=1<<(n*2);
for (int i=1; i<=n; ++i) lid[i]=tot++;
for (int i=1; i<=n; ++i) rid[i]=tot++;
for (int i=1; i<=n; ++i)
for (int j=1; j<=a[i]; ++j) e[lid[i]].pb(rid[j]), e[rid[j]].pb(lid[i]);
for (int i=0; i<tot; ++i) {
// cout<<"i: "<<i<<endl;
memset(f, 0, sizeof(f));
f[1<<i][i]=1;
for (int s=0,cnt; s<lim; ++s) {
cnt=__builtin_popcount(s);
for (int j=0; j<tot; ++j) if (f[s][j]) {
for (auto& v:e[j]) if (v<=i)
if (v==i && cnt>2) md(ans, f[s][j]);
else if (!(s&(1<<v))) md(f[s|(1<<v)][v], f[s][j]);
}
}
}
ans=1ll*ans*inv2%mod;
cout<<ans<<endl;
}
}
namespace task1{
ll f[N], ans;
inline void add(ll& a, ll b) {a=(a+b)%mod;}
void solve() {
sort(a+1, a+n+1);
f[0]=1;
for (int i=1; i<=n; ++i) {
// cout<<"i: "<<i<<endl;
for (int j=a[i-1]+1; j<=a[i]; ++j)
for (int k=j; k; --k)
add(f[k], f[k-1]);
// cout<<"f: "; for (int j=1; j<=a[i]; ++j) cout<<f[j]<<' '; cout<<endl;
add(ans, f[1]);
for (int j=2; j<=a[i]; ++j) add(f[j-1], f[j]*j%mod*(j-1));
}
for (int i=1; i<=n; ++i) ans=(ans-a[i])%mod;
ans=ans*inv2%mod;
cout<<(ans%mod+mod)%mod<<endl;
}
}
signed main()
{
freopen("ring.in", "r", stdin);
freopen("ring.out", "w", stdout);
n=read();
for (int i=1; i<=n; ++i) a[i]=read();
// force::solve();
task1::solve();
return 0;
}