列表

详情


NC219804. SetUpTheFlag

描述

You are given a matrix of rows and columns, for each grid , there is an integer on it, and the other grids are empty.
                                        
                        One of the way to set up flags in the second sample.
You have to set up flags in empty grids. For the grid with an integer on it, the amount of flags in the surrounding grids needs to be equal to this integer.
Please calculate how many ways to set up the flag, two ways are considered different if and only if at least one flag is seted up in a different grid.

输入描述

The first line contains an integer  — the number of test cases, each test case given a matrix of  rows and  columns,  representing the integer on grid  .

输出描述

For each test case, please output an integer to indicate how many ways to set up the flags.

示例1

输入:

2
1 0 0
0 0 0
0 0 0
2 3 3
1 2 1
1 1 1

输出:

3
34542

说明:

For the first example, there are only three ways to set up the flag:
We can set up the flag at (1,1)_{} or (1,2)_{} or (2,1)_{} , and the other grid must be empty.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 171ms, 内存消耗: 2740K, 提交时间: 2021-03-23 18:41:28

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5 + 7;

int t;
int a[10], m[10][10][10][10][10][10];
inline int cal(int x, vector<int> v)
{
    int sum = 0;
    for(auto i : v) sum += (x >> i) & 1;
    return sum;
}

void init()
{
    int a1, a2, a3, a4, a5, a6;
    for (int i = 0; i < (1ll << 20);i++)
    {
        a1 = cal(i, {0, 1, 2, 7, 8, 11, 12, 13});
        a2 = cal(i, {2, 3, 4, 8, 9, 13, 14, 15});
        a3 = cal(i, {4, 5, 6, 9, 10, 15, 16, 17});
        a4 = cal(i, {11, 12, 13, 18, 19});
        a5 = cal(i, {13, 14, 15, 19});
        a6 = cal(i, {15, 16, 17});
        m[a1][a2][a3][a4][a5][a6]++;
    }
}


int main()
{
    init();
    scanf("%d", &t);
    while(t--)
    {
        for (int i = 1; i <= 9;i++) scanf("%d", &a[i]);
        ll ans = 0;

        for (int i = 0; i <= min(3, a[4]);i++)
        {
            for (int j = 0; j <= min(a[5], 6);j++)
            {
                for (int z = 0; z <= min(6, a[6]);z++)
                {
                    ans += 1ll * m[a[1]][a[2]][a[3]][a[4] - i][a[5] - j][a[6] - z] * m[a[9]][a[8]][a[7]][z][j][i];
                }
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

上一题