【LOJ #6072】 「2017 山东一轮集训 Day5」苹果树(容斥 / 搜索 / 矩阵树定理)
考虑求出个好苹果时权值和的方案数
这个可以用折半搜索+双指针求出来
设权值不为的苹果有个
然后考虑把苹果分成三部分:
那么就是之间连边
这样求出来是至多个苹果的方案
再二项式反演一下即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline void readstring(vector<char> &s){
char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s.pb(ch),ch=gc();
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=45;
int val[N];
vector<int> s1[N],s2[N];
int f[N][N],C[N][N];
int g[N],coef[N],a[N],n,m,lim;
inline void init_inv(){
for(int i=0;i<N;i++){
C[i][0]=C[i][i]=1;
for(int j=1;j<i;j++)C[i][j]=add(C[i-1][j],C[i-1][j-1]);
}
}
inline int calc(int x,int y){
int res=0;
for(int i=0,j=(int)s2[y].size()-1;i<s1[x].size();i++){
while((~j)&&s1[x][i]+s2[y][j]>lim)j--;
Add(res,j+1);
}
return res;
}
inline void addedge(int u,int v){
f[u][u]++,f[v][v]++,f[u][v]--,f[v][u]--;
}
inline int calc(int n){
int res=1;
for(int i=1;i<=n;i++){
int pos=i;
for(;pos<=n;pos++)if(f[pos][i])break;
if(pos!=i)swap(f[pos],f[i]),res=mod-res;
int iv=Inv(f[i][i]);
for(int j=i+1;j<=n;j++){
int mt=mul(f[j][i],iv);
for(int k=i;k<=n;k++)
Dec(f[j][k],mul(f[i][k],mt));
}
}
for(int i=1;i<=n;i++)Mul(res,f[i][i]);
return res;
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
init_inv();
n=read(),lim=read();
for(int i=1;i<=n;i++){
a[i]=read();
if(a[i]!=-1)a[++m]=a[i];
}
for(int i=0;i<(1<<(m/2));i++){
int siz=0,sum=0;
for(int j=0;j<m/2;j++)if(i&(1<<j))
siz++,sum+=a[j+1];
s1[siz].pb(sum);
}
for(int i=0;i<(1<<(m-m/2));i++){
int siz=0,sum=0;
for(int j=0;j<(m-m/2);j++)if(i&(1<<j))
siz++,sum+=a[m/2+j+1];
s2[siz].pb(sum);
}
for(int i=0;i<=m/2;i++)sort(s1[i].bg(),s1[i].end());
for(int i=0;i<=m-m/2;i++)sort(s2[i].bg(),s2[i].end());
for(int i=0;i<=m/2;i++)
for(int j=0;j<=(m-m/2);j++)
Add(coef[i+j],calc(i,j));
for(int i=0;i<=n;i++){
memset(f,0,sizeof(f));
for(int j=1;j<=i;j++){
for(int k=j+1;k<=i;k++)addedge(j,k);
for(int k=m+1;k<=n;k++)addedge(j,k);
}
for(int j=i+1;j<=m;j++)
for(int k=m+1;k<=n;k++)addedge(j,k);
for(int j=m+1;j<=n;j++)
for(int k=j+1;k<=n;k++)addedge(j,k);
g[i]=calc(n-1);
}
int ans=0;
for(int i=0;i<=n;i++){
int res=g[i];
for(int j=0;j<i;j++){
int now=mul(C[i][j],g[j]);
if((i-j)&1)now=mod-now;
Add(res,now);
}
Add(ans,mul(res,coef[i]));
}
cout<<ans<<'\n';
}