链接
题解
状压 d p dp dp
f i j f_{ij} fij表示考虑了前 i i i行,当前选择的列的状态为 j j j
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 4200000
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
#define mod 1000000007ll
ll n, f[2][maxn], a[30][30], num[maxn];
#define lowbit(x) ((x)&(-x))
int main()
{
freopen("permanent.in","r",stdin);
freopen("permanent.out","w",stdout);
ll i, j, s, k, mask;
n=read(), mask = (1ll<<n)-1;
rep(i,1,n)rep(j,1,n)a[i][j]=read();
rep(i,1,n)num[1ll<<i-1]=i;
f[0][0]=1;
rep(i,0,n-1)
{
cl(f[i&1^1]);
rep(s,0,mask)
{
f[i&1][s]%=mod;
if(f[i&1][s])
{
ll* g = f[i&1^1];
ll tmp = mask & ~s;
while(tmp)
{
ll t = lowbit(tmp);
g[s|t] += f[i&1][s] * a[i+1][num[t]];
tmp -= t;
}
}
}
}
printf("%lld",f[n&1][mask]%mod);
return 0;
}