题解:P14990 马赛克

· · 题解

考虑对于 (x,y),若 x \in S \lor y \in T,则称该格子为可操作的。显然,我们把所有可操作的格子都涂成同色,是最优策略。

此时有可能有贡献的四元组 (a,b,c,d) 满足 a,b \notin S \land c,d \notin T。

考虑 g(S,T) = \left(\complement_{\mathbb U_n} S \right)\oplus \left( \complement_{\mathbb U_m} T \right)(此处 \oplus 表示二进制按位拼接)表示集合 (S,T),并用一个 [0,2^{n+m}) 的二进制数表示 (a,b,c,d)。具体地,令从高到低第 a,b,n+c,n+d 位记作 1,其余记作 0。

则对当前枚举的集合 (S,T) 有贡献的四元组一定是 g(S,T) 的子集,所以可以 FMT 实现高维前缀和,复杂度 \mathcal O((n+m)2^{n+m}),可以通过本题。

其实不用太卡常

#include<bits/stdc++.h>
#define ull unsigned long long
#define rep(x,a,b) for(int x=a;x<=b;x++)
using namespace std;

const int N=13,MT=4;
int n,m,k,w[MT][114514],fmt[MT][1<<24];
bool ch[N][N];

static inline ull qp(ull a,int b){
    ull ret=1;
    for(;b;b>>=1,a*=a)
        if(b&1)ret*=a;
    return ret;
}

static inline int gets(int a,int b,int c,int d){
    int ret=0;
    ret|=(1<<(m-d));
    ret|=(1<<(m-c));
    ret|=(1<<(n+m-b));
    ret|=(1<<(n+m-a));
    return ret;
}

static inline void solve(int tid){
    cin>>n>>m>>k;
    rep(i,1,n)rep(j,1,m)cin>>ch[i][j];
    const int total=1<<(n+m);
    int*p=fmt[tid];
    rep(a,1,n)rep(b,a+1,n)rep(c,1,m)rep(d,c+1,m)
        if(ch[a][c]==ch[b][d]&&ch[b][c]==ch[a][d]&&ch[a][c]!=ch[a][d])
            p[gets(a,b,c,d)]=1;
    for(int i=1;i<total;i<<=1)
    for(int j=0;j<total;j+=i<<1)
    rep(k,0,i-1)
        p[i+j+k]+=p[j+k];
    rep(i,0,total-1)w[tid][ p[i] ]++;
    ull ans=0;
    rep(i,0,50000)if(w[tid][i])ans+=qp(w[tid][i],k);
    cout<<ans<<'\n';
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;cin>>T;
    rep(i,0,T-1)solve(i);
}