「题解」ABC288G 3^N Minesweeper

· · 题解

会推 fwt 就能直接秒/jy

先考虑 B 进行个什么线性变换才能得到 A.fwt 要做的是:枚举每一维,然后固定其它维的值看作常数,然后将这一维上的值单独拎出来乘上一个矩阵。那么只需要构造 n=3 的矩阵,然后每次做这样一个过程就能完成 n=3^k.

构造就挺简单的了:

\begin{bmatrix} 1 & 1 & 0\\ 1 & 1 & 1\\ 0 & 1 & 1 \end{bmatrix} \begin{bmatrix} b_0\\ b_1\\ b_2 \end{bmatrix} = \begin{bmatrix} b_0+b_1\\ b_0+b_1+b_2\\ b_1+b_2 \end{bmatrix}

这是 B 到 A 要做的事情。而 A 转成 B 每次就乘上它的逆矩阵就行了。

\begin{bmatrix} 0 & 1 & -1\\ 1 & -1 & 1\\ -1 & 1 & 0 \end{bmatrix}
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
    r=0;bool w=0;char ch=getchar();
    while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
    while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
    return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
    int s=1;
    while(y){
        if(y&1)s=1ll*s*x%mod;
        x=1ll*x*x%mod;
        y>>=1;
    }
    return s;
}
const int N=600010;
int n,fpow[14];
int f[N],a[3][3],b[3],c[3];
signed main(){
    #ifdef do_while_true
//      assert(freopen("data.in","r",stdin));
//      assert(freopen("data.out","w",stdout));
    #endif
    a[0][0]=0;a[0][1]=1;a[0][2]=-1;
    a[1][0]=1;a[1][1]=-1;a[1][2]=1;
    a[2][0]=-1;a[2][1]=1;a[2][2]=0;
    read(n);
    fpow[0]=1;for(int i=1;i<=n;i++)fpow[i]=fpow[i-1]*3;
    for(int i=0;i<fpow[n];i++)read(f[i]);
    for(int i=0;i<n;i++){
        for(int j=0;j<fpow[n];j++){
            if((j/fpow[i])%3==0){
                c[0]=c[1]=c[2]=0;
                b[0]=f[j];b[1]=f[j+fpow[i]];b[2]=f[j+fpow[i]*2];
                for(int p=0;p<3;p++)
                    for(int q=0;q<3;q++)
                        c[p]+=a[p][q]*b[q];
                f[j]=c[0];f[j+fpow[i]]=c[1];f[j+fpow[i]*2]=c[2];
            }
        }
    }
    for(int i=0;i<fpow[n];i++)cout << f[i] << ' ';
    #ifdef do_while_true
        cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
    #endif
    return 0;
}