【BZOJ2024】舞会(动态规划,容斥,高精度)
【BZOJ2024】舞会(动态规划,容斥,高精度)
题面
题解
这种关系显然要先排序才不会不想影响。
设\(f[i][j]\)表示前\(i\)个女生中,选了\(j\)个女生配对,并且女生比男生高,剩余的随意匹配的方案数。
转移:
\(f[i][j]=f[i-1][j]+f[i-1][j-1]*(p-(j-1)\)
然后容斥一下即可,高精度常数巨大。
\(BZOJ\)似乎过不了???
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 205
const int yw=10000;
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct BigNum
{
ll s[MAX*2];int ws;
void output()
{
printf("%lld",s[ws]);
for(int i=ws-1;i;--i)
printf("%04lld",s[i]);
puts("");
}
void clear(){memset(s,0,sizeof(s));ws=0;}
}f[MAX][MAX],ans,C[MAX][MAX],fac[MAX];
BigNum operator+(BigNum a,BigNum b)
{
int ws=max(a.ws,b.ws);BigNum ret;ret.clear();
for(int i=1;i<=ws;++i)ret.s[i]=a.s[i]+b.s[i];
for(int i=1;i<=ws;++i)ret.s[i+1]+=ret.s[i]/yw,ret.s[i]%=yw;
while(ret.s[ws+1])++ws,ret.s[ws+1]+=ret.s[ws]/yw,ret.s[ws]%=yw;
ret.ws=ws;return ret;
}
BigNum operator-(BigNum a,BigNum b)
{
int ws=max(a.ws,1);BigNum ret;ret.clear();
for(int i=1;i<=ws;++i)ret.s[i]=a.s[i]-b.s[i];
for(int i=ws-1;i;--i)if(ret.s[i]<0)ret.s[i]+=yw,ret.s[i+1]-=1;
while(!ret.s[ws])--ws;
ret.ws=ws;return ret;
}
BigNum operator*(BigNum a,int b)
{
int ws=a.ws;BigNum ret;ret.clear();
for(int i=1;i<=ws;++i)ret.s[i]=a.s[i]*b;
for(int i=1;i<=ws;++i)ret.s[i+1]+=ret.s[i]/yw,ret.s[i]%=yw;
while(ret.s[ws+1])++ws,ret.s[ws+1]+=ret.s[ws]/yw,ret.s[ws]%=yw;
ret.ws=ws;return ret;
}
BigNum operator*(BigNum a,BigNum b)
{
int ws=a.ws+b.ws-1;BigNum ret;ret.clear();
for(int i=1;i<=a.ws;++i)
for(int j=1;j<=b.ws;++j)
ret.s[i+j-1]+=a.s[i]*b.s[j];
for(int i=1;i<=ws;++i)ret.s[i+1]+=ret.s[i]/yw,ret.s[i]%=yw;
while(ret.s[ws+1])++ws,ret.s[ws+1]+=ret.s[ws]/yw,ret.s[ws]%=yw;
ret.ws=ws;return ret;
}
int n,k,a[MAX],b[MAX],p[MAX];
int main()
{
n=read();k=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<=n;++i)b[i]=read();
sort(&a[1],&a[n+1]);sort(&b[1],&b[n+1]);
for(int i=1,j=0;i<=n;++i)
{
while(j<n&&a[j+1]<b[i])++j;
p[i]=j;
}
C[0][0].ws=C[1][0].ws=C[1][1].ws=1;
C[0][0].s[1]=C[1][0].s[1]=C[1][1].s[1]=1;
for(int i=2;i<=n;++i)
{
C[i][0].s[1]=C[i][0].ws=1;
for(int j=1;j<=i;++j)C[i][j]=C[i-1][j]+C[i-1][j-1];
}
fac[0].ws=fac[0].s[1]=1;
for(int i=1;i<=n;++i)
fac[i]=fac[i-1]*i;
f[0][0].ws=f[0][0].s[1]=1;
for(int i=1;i<=n;++i)
for(int j=0;j<=i;++j)
{
f[i][j]=f[i-1][j];
if(j&&p[i]-j+1>0)f[i][j]=f[i][j]+f[i-1][j-1]*(p[i]-j+1);
}
for(int i=0;i<=n;++i)f[n][i]=f[n][i]*fac[n-i];
for(int i=n;~i;--i)
for(int j=i+1;j<=n;++j)
f[n][i]=f[n][i]-C[j][i]*f[n][j];
for(int i=0;i<=k;++i)ans=ans+f[n][i];
ans.output();
return 0;
}