题目
枚举与值,枚举步数和最后一个点DP,枚举前一个点转移,只把起点都终点符合与值的方案计算。
最后得到的是与值为其超集的方案数,容斥一下(你也可以叫这IFMT)就可以得出答案。
时间复杂度 O ( n 3 m ) O(n^3m) O(n3m)
循环展开+使用 unsigned long long \texttt{unsigned long long} unsigned long long代替 long long \texttt{long long} long long 获得 1000ms \texttt{1000ms} 1000ms加速即可AC此题。
循环展开太强辣
AC Code:
#include
#define maxn 64
#define maxm 20005
#define mod 998244353
#define LL unsigned long long
using namespace std;
int n, m, A[maxn][maxn], B[maxn][maxn], S[maxn], f[2][maxn], ans[maxn][maxm];
inline int add(int a, int b) {
int c = a + b;
return c >= mod ? c - mod : c;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &A[i][j]);
for (int x = 0; x < n; x++) {
memset(f, 0, sizeof f);
memset(S, 0, sizeof S);
int now = 1, pre = 0;
for (int i = 0; i < n; i++) {
if ((i & x) == x) {
for (int j = 0; j < n; j++) S[j] = add(S[j], A[i][j]);
f[pre][i] = 1;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) B[i][j] = add(A[i][j], ((i & x) == x) * S[j]);
for (int i = 0; i < n; i++) {
LL ret = 0;
for (int k = 0; k < n; k += 8) {
ret += (LL)f[pre][k] * A[k][i], ret += (LL)f[pre][k + 1] * A[k + 1][i],
ret += (LL)f[pre][k + 2] * A[k + 2][i], ret += (LL)f[pre][k + 3] * A[k + 3][i],
ret += (LL)f[pre][k + 4] * A[k + 4][i], ret += (LL)f[pre][k + 5] * A[k + 5][i],
ret += (LL)f[pre][k + 6] * A[k + 6][i], (ret += (LL)f[pre][k + 7] * A[k + 7][i]) %= mod;
}
f[now][i] = ret;
if ((i & x) == x)
ans[x][1] = add(ans[x][1], ret);
}
swap(now, pre);
for (int j = 2; j <= m; j++, swap(now, pre))
for (int i = 0; i < n; i++) {
LL ret = 0;
for (int k = 0; k < n; k += 8) {
ret += (LL)f[pre][k] * B[k][i], ret += (LL)f[pre][k + 1] * B[k + 1][i],
ret += (LL)f[pre][k + 2] * B[k + 2][i], ret += (LL)f[pre][k + 3] * B[k + 3][i],
ret += (LL)f[pre][k + 4] * B[k + 4][i], ret += (LL)f[pre][k + 5] * B[k + 5][i],
ret += (LL)f[pre][k + 6] * B[k + 6][i],
(ret += (LL)f[pre][k + 7] * B[k + 7][i]) %= mod;
}
f[now][i] = ret;
if ((i & x) == x)
ans[x][j] = add(ans[x][j], ret);
}
}
for (int bit = 1; bit < n; bit <<= 1)
for (int i = 0; i < n; i++)
if (i & bit)
for (int j = 1; j <= m; j++) ans[i - bit][j] = add(ans[i - bit][j], mod - ans[i][j]);
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 1; j <= m; j++) res ^= ans[i][j];
printf("%d\n", res);
}
正解:就是上面的 d p dp dp只不过我们对于步数只 d p dp dp到 2 n + 1 2n+1 2n+1步,然后对于每个值拿 a [ 0...2 n + 1 ] a[0...2n+1] a[0...2n+1]做 B M BM BM然后就可以 O ( n 2 m ) O(n^2m) O(n2m)求出与值为其超集的方案数。
注意是一次旅行是 n n n阶递推,也就是说一次旅行是可以写作 S ( x ) R ( x ) \frac {S(x)}{R(x)} R(x)S(x)的形式,其中 R ( x ) R(x) R(x)的次数 ≤ n \leq n ≤n, S ( x ) S(x) S(x)的次数 ≤ n − 1 \leq n-1 ≤n−1,那么多次旅行就是 1 1 − S ( x ) R ( x ) = R ( x ) R ( x ) − S ( x ) \frac 1{1 - \frac {S(x)}{R(x)}} = \frac {R(x)}{R(x) - S(x)} 1−R(x)S(x)1=R(x)−S(x)R(x),所以分子的次数是可以达到 n n n次的,所以多次旅行是 n + 1 n+1 n+1阶递推,需要前 2 n + 1 2n+1 2n+1项(虽然实际测试 2 n 2n 2n项就可以过,感觉是因为最高次是分子的原因。)
C o d e Code Code
#include
#define maxn 65
#define maxm 20005
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);j>=LIM;j--)
#define mod 998244353
#define vi vector
#define pb push_back
#define poly vi
using namespace std;
int S;
int n,L,m,a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],g[maxn<<1][maxn],f[maxm][maxn];
int Pow(int b,int k){
int r=1;for(;k;k>>=1,b=1ll*b*b%mod) if(k&1) r=1ll*r*b%mod;return r; }
void FMT(int *a,int tp){
rep(j,0,L-1) rep(i,0,n-1)
if(i >> j & 1){
int v = (i ^ (1 << j));
if(tp == 1) a[v] = (a[v] + a[i]) % mod;
else a[v] = (a[v] - a[i]) % mod;
}
}
vi BM(vi &a){
vi r(1,1),p,t;int l;
rep(i,0,a.size()-1){
int b=0;
rep(j,0,r.size()-1) b = (b + 1ll * r[j] * a[i-j]) % mod;
if(!b) continue;
t = r , r.resize(max(r.size() , i+2 - (r.size() - (r.size() != 0))));
rep(j,0,p.size()-1) r[i-l+j] = (r[i-l+j] - 1ll * p[j] * b) % mod;
p = t;int iv = Pow(b , mod-2);
rep(j,0,p.size()-1) p[j] = 1ll * p[j] * iv % mod;
l = i;
}
return r;
}
int main(){
scanf("%d%d",&n,&m);
for(L=0;(1<<L) < n;L++);
rep(i,0,n-1) rep(j,0,n-1) scanf("%d",&a[i][j]);
rep(i,0,n-1) b[i][i] = 1;
rep(i,0,n-1) f[0][i] = 1;
int S = n * 2 + 1;
rep(i,1,S){
rep(j,0,n-1) rep(k,0,n-1) rep(p,0,n-1) c[j][p] = (c[j][p] + 1ll * a[j][k] * b[k][p]) % mod;
rep(j,0,n-1) rep(k,0,n-1) g[i][j&k] = (g[i][j&k] + (b[j][k] = c[j][k])) % mod , c[j][k] = 0;
FMT(g[i],1);
}
rep(i,1,S) rep(j,1,i) rep(k,0,n-1) f[i][k] = (f[i][k] + 1ll * f[i-j][k] * g[j][k]) % mod;
rep(i,0,n-1){
vi a;
rep(j,0,S) a.pb(f[j][i]);
vi P = BM(a);
rep(j,S+1,m){
f[j][i] = 0;
rep(k,1,P.size()-1)
f[j][i] = (f[j][i] - 1ll * f[j-k][i] * P[k]) % mod;
}
}
int ans = 0;
rep(i,1,m) FMT(f[i],-1);
rep(i,1,m) rep(j,0,n-1) ans ^= (f[i][j] + mod) % mod;
printf("%d\n",ans);
}