有点费肝
2023-08-15 10:42:08
发布于:广东
5阅读
0回复
0点赞
#include <bits/stdc++.h>
#define in read<int>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)
using namespace std;
using ll = long long;
template < typename T > T read() {
T x = 0; bool f = 0; char ch = getchar();
while(!isdigit(ch)) f |= ch == '-', ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = (1 << 18) + 10;
ll f[N][19], g[N], h[N];
int n, Q;
char s[20];
int p[20][20];
bool avi[20][20];
int lg[N];
int main() {
n = in;
rep(i, 0, n - 1) {
rep(j, 0, n - 1) p[i][j] = in - 1;
rep(j, 0, n - 1) {
avi[i][p[i][j]] = true;
if(p[i][j] == i) break;
}
} int U = 1 << n; U--;
rep(i, 0, n - 1) f[1 << i][i] = 1;
rep(i, 2, U) lg[i] = lg[i >> 1] + 1;
rep(s, 1, U)
rep(ed, 0, n - 1) if(f[s][ed]) {
int x = lg[s & -s];
rep(i, x, n - 1) {
if(i == x && avi[ed][i]) g[s] += f[s][ed];
if(~s >> i & 1 && avi[ed][i])
f[s | 1 << i][i] += f[s][ed];
}
}
h[0] = 1;
rep(s, 1, U) {
int x = lg[s & -s];
for(int t = s; t; t = (t - 1) & s) if(t >> x & 1)
h[s] = h[s] + g[t] * h[s ^ t];
}
Q = in;
while(Q--) {
scanf("%s", s);
int stu = 0; rep(i, 0, n - 1) if(s[i] == 'H') stu |= 1 << i;
ll ans = h[stu] * h[U ^ stu];
printf("%lld\n", ans);
}
return 0;
}
这里空空如也
有帮助,赞一个