题解 sort
小清新神仙题目
直接处理排列很难处理
\(\forall x\in[1, n]\),考虑 01 序列 \(t_i=[a_i\geqslant x]\)
对这个 01 序列状压求答案是容易的
那么对所有 01 序列答案求和即可
复杂度 \(O(nm2^n)\)
点击查看代码
// ubsan: undefined
// accoders
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#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, m;
int a[N], now;
ll f[2][1<<15], ans[N];
const ll mod=12345678, p=233;
inline void md(ll& a, ll b) {a+=b; a=a>=mod?a-mod:a;}
signed main()
{
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
n=read(); m=read();
for (int i=0; i<n; ++i) a[i]=read();
int lim=1<<n;
for (int x=1; x<=n; ++x) {
int s=0;
for (int i=0; i<n; ++i) s|=(a[i]>=x)<<i;
f[now][s]=1;
}
for (int i=1; i<=m; ++i) {
for (int j=1; j<n; ++j,now^=1) {
memset(f[now^1], 0, sizeof(f[now^1]));
for (int s=0; s<lim; ++s) if (f[now][s]) {
bool t1=s&(1<<j-1), t2=s&(1<<j);
if (t1^t2) {
if (t1<t2) {
f[now^1][s]=(f[now^1][s]+f[now][s]*(1-p))%mod;
f[now^1][s^(3<<j-1)]=(f[now^1][s^(3<<j-1)]+f[now][s]*p)%mod;
}
else {
f[now^1][s]=(f[now^1][s]+f[now][s]*p)%mod;
f[now^1][s^(3<<j-1)]=(f[now^1][s^(3<<j-1)]+f[now][s]*(1-p))%mod;
}
}
else md(f[now^1][s], f[now][s]);
}
}
}
for (int s=0; s<lim; ++s)
for (int j=0; j<n; ++j) if (s&(1<<j))
md(ans[j], f[now][s]);
for (int i=0; i<n; ++i) printf("%lld%c", (ans[i]%mod+mod)%mod, " \n"[i==n-1]);
return 0;
}