这道题先看数据范围,发现 ,所以考虑状压 dp。
根据题意,可以枚举一个 表示把 作为镜子,能满足 的 的集合, 为状压的选择的下标。
此时已经预处理出来的是一段一段的集合,此时定义 表示强制选择第 个数,选择集合为 的数量,用预处理数组去 dp 即可,如:
主要思路就是这样了,就是这样子弄的话,可能会有重复,所以可以排一个序,每次只从前面的数字转移,这样就能解决。还有就是若有两个相同的数字,就像样例二一样,此时可以在后一个数字计算时加上两个前面的贡献。
#include<bits/stdc++.h>
#define int long long
#define up(i,x,y) for(register int i=x;i<=y;i=-~i)
#define dn(i,y,x) for(register int i=y;i>=x;i--)
#define mst(x,y) memset(x,y,sizeof x)
#define D(x) cout<<#x<<": "<<x<<endl;
#define DE(x) cout<<#x<<": "<<x<<" ";
#define MAXSIZE 1<<21
// #define faster 1
#define pl p<<1
#define pr p<<1|1
using namespace std;
namespace lry{
char buf[MAXSIZE],*p1=buf,*p2=buf;
char pbuf[MAXSIZE],*pp=pbuf;
inline int gc(){
#if faster
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
#else
return getchar();
#endif
}
inline void pc(const char &c){
#if faster
if(pp-pbuf==MAXSIZE) fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;*pp++=c;
#else
putchar(c);
#endif
}
inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
inline void read(double &rdx){double t=0;int x=0,s=0,f=1;char c;do c=gc();while(!isdigit(c)&&c!='-'&&c!='.');if (c=='-')f=-1,c=gc();while(isdigit(c)&&c!='.')x=(c^48)+(x<<1)+(x<<3),c=gc();if(c=='.')c=gc();else{rdx=x*f;return;}while(c>='0'&&c<='9')t=t*10+(c^48),s=-~s,c=gc();while(s--)t/=10;rdx=(x+t)*f;}
inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
inline void read(char &x){char a=gc();for(;blank(a)&&(a^-1);a=gc());if(!blank(a)&&(a^-1))x=gc();}
inline void read(int &x){int ret=0,f=0;char ch=gc();while(!isdigit(ch)){if(ch=='-')f=1;ch=gc();}while(isdigit(ch)){ret=(ret<<1)+(ret<<3)+(ch^48);ch=gc();}x=(f?-ret:ret);}
template<typename t,typename ...T> inline void read(t &x,T&...y){read(x),read(y...);}
inline void write(int x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+48);}
inline void write(const char *x){fputs(x,stdout);}
inline void write(const string &x){fputs(x.c_str(),stdout);}
inline void writeln(int x){write(x),pc('\n');}
inline void writetr(int x){write(x),pc(' ');}
inline void writeln(string x){write(x),pc('\n');}
inline void writetr(string x){write(x),pc(' ');}
template<typename t,typename ...T> inline void write(t x,T...y){write(x),write(y...);}
}
using namespace lry;
constexpr int N=15+10,M=15,inf=0x3f3f3f3f3f3f3f3f,mod=1000000007;
int n,a[N],f[1<<M];
vector<int> vc[N];
signed main(){
read(n);
for(int i=1;i<=n;i++) read(a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
for(int j=0;j<(1<<i);j++){
if((j>>(i-1))&1) continue;
int sum=0,cnt=0;
for(int k=0;k<n;k++){
if((j>>k)&1) sum+=a[k+1],cnt++;
}
if(sum==a[i]){
vc[i].push_back(j|(1<<(i-1)));
if(cnt==1) vc[i].push_back(j|(1<<(i-1)));
}
}
vc[i].push_back(1<<(i-1));
}
f[0]=1;
for(int i=1;i<=n;i++){
for(int j=(1<<i)-1;j>=0;j--){
for(int l:vc[i]){
if(j&l) continue;
(f[j|l]+=f[j])%=mod;
}
}
}
writeln(f[(1<<n)-1]);
#if faster
fwrite(pbuf,1,pp-pbuf,stdout);
pp=pbuf;
#endif
return 0;
}