1 solutions

  • -2
    @ 2026-6-20 14:11:41

    题目解析

    首先一个条件是每个 2×22 \times 2 的子矩形,里面的红色和蓝色个数是一样多的。如果棋盘中有两个相邻的元素同色,那么有如下模式:

    ????BB????
    ????RR????
    ????BB????
    ????RR????
    ...
    

    同时,对于每一列也一定是 RB 交替的,这样也一定满足条件。如果没有相邻的同色,那么整个矩形是红蓝相间的,也满足条件。

    关键转化: 把所有 (i,j)(i, j) 满足 i+ji + j 是偶数的位置,颜色红蓝反转,那么一定是所有行都同色或者所有列都同色。

    于是可以计算所有行同色的方案和所有列同色的方案,减去两个都同色的情况(容斥原理)。

    算法步骤:

    1. 对于每个格子 (i,j)(i, j),若 i+ji+j 为奇数,将颜色反转(R 变 B,B 变 R)。
    2. 统计每行中已确定的颜色类型,如果某行中同时出现 R 和 B,则该行无法满足"行同色"条件,标记 nr = -1;否则若该行尚未确定颜色,则 nr--(表示该行可自由选择)。
    3. 同理统计每列,记录 nc。
    4. 同时记录全局是否所有行列都同色(flag),用于容斥去重。
    5. 最终答案:
      • 如果 nr >= 0:行方案数 = 2nr2^{nr}。
      • 如果 nc >= 0:列方案数 = 2nc2^{nc}。
      • 如果 flag != -1:减去重复计算的方案(若 flag == 0 减 2,否则减 1)。

    时间复杂度:O(nm)O(nm)。


    参考代码

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <ctime>
    using namespace std;
    typedef long long ll;
    #define maxn 1005
    #define mod 1000000007
    int T, n, m, row[maxn], col[maxn], nr, nc, flag;
    char s[maxn];
    
    void inc(int &a, int b) {
        a = a + b >= mod ? a + b - mod : a + b;
    }
    
    void dec(int &a, int b) {
        a = a - b < 0 ? a - b + mod : a - b;
    }
    
    int mod_pow(int a, int b) {
        int ans = 1;
        while (b) {
            if (b & 1) ans = (ll)ans * a % mod;
            a = (ll)a * a % mod;
            b >>= 1;
        }
        return ans;
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        nr = n, nc = m, flag = 0;
        for (int i = 1; i <= n; i++) row[i] = 0;
        for (int i = 1; i <= m; i++) col[i] = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            for (int j = 1; j <= m; j++)
                if (s[j] != '?' && !(nr == -1 && nc == -1)) {
                    int type;
                    if (s[j] == 'R') type = 1;
                    else type = 2;
                    if ((i + j) & 1) type = 3 - type;
                    if (row[i] == 3 - type)
                        nr = -1;
                    else if (!row[i])
                        nr--, row[i] = type;
                    if (col[j] == 3 - type)
                        nc = -1;
                    else if (!col[j])
                        nc--, col[j] = type;
                    if (flag != -1)
                        if (flag == 3 - type)
                            flag = -1;
                        else
                            flag = type;
                }
        }
        int ans = 0;
        if (nr >= 0) inc(ans, mod_pow(2, nr));
        if (nc >= 0) inc(ans, mod_pow(2, nc));
        if (flag != -1) {
            if (flag == 0) dec(ans, 2);
            else dec(ans, 1);
        }
        printf("%lld\n", ans);
        return 0;
    }
    
    • 1

    Information

    ID
    193
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    5
    Accepted
    2
    Uploaded By