A. The Very Beautiful Blanket(div1)(按位异或性质)

文章描述了一个编程挑战,Kirill想要编织一个美丽的毯子,其由4x4的方块组成,每个方块代表一个非负整数。毯子被认为是美丽的,如果每个4x4子矩阵满足特定的异或条件。给定毯子的尺寸,任务是生成一个矩阵,最大化不同数字的数量同时满足条件。输入包含测试用例数量及毯子尺寸,输出应为满足条件的最大不同数字数量及对应的矩阵。
摘要由CSDN通过智能技术生成

Kirill wants to weave the very beautiful blanket consisting of 𝑛×𝑚 of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a 𝐵 matrix of size 𝑛×𝑚 consisting of non-negative integers.

Kirill considers that the blanket is very beautiful, if for each submatrix 𝐴 of size 4×4 of the matrix 𝐵 is true:

  • 𝐴11⊕𝐴12⊕𝐴21⊕𝐴22=𝐴33⊕𝐴34⊕𝐴43⊕𝐴44,
  • 𝐴13⊕𝐴14⊕𝐴23⊕𝐴24=𝐴31⊕𝐴32⊕𝐴41⊕𝐴42,

where ⊕⊕ means bitwise exclusive OR

Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible!

He gives you two integers 𝑛and 𝑚.

Your task is to generate a matrix 𝐵 of size 𝑛×𝑚, which corresponds to a very beautiful blanket and in which the number of different numbers maximized.

Input

The first line of input data contains one integer number 𝑡 (1≤𝑡≤1000) — the number of test cases.

The single line of each test case contains two integers 𝑛 and 𝑚 (4≤𝑛,𝑚≤200)— the size of matrix 𝐵.

It is guaranteed that the sum of 𝑛⋅𝑚 does not exceed 2⋅105

Output

For each test case, in first line output one integer 𝑐𝑛𝑡 (1≤𝑐𝑛𝑡≤𝑛⋅𝑚) — the maximum number of different numbers in the matrix.

Then output the matrix 𝐵 (0≤𝐵𝑖𝑗<263) of size 𝑛×𝑚. If there are several correct matrices, it is allowed to output any one.

It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a 𝐵 that (0≤𝐵𝑖𝑗<263).

Example

input

Copy

 
 

4

5 5

4 4

4 6

6 6

output

Copy

25
9740 1549 9744 1553 9748
1550 1551 1554 1555 1558
10252 2061 10256 2065 10260
2062 2063 2066 2067 2070
10764 2573 10768 2577 10772
16
3108 3109 3112 3113
3110 3111 3114 3115
3620 3621 3624 3625
3622 3623 3626 3627
24
548 549 552 553 556 557
550 551 554 555 558 559
1060 1061 1064 1065 1068 1069
1062 1063 1066 1067 1070 1071
36
25800 25801 25804 25805 25808 25809
25802 4294993099 25806 4294993103 25810 4294993107
26312 26313 26316 26317 26320 26321
26314 4294993611 26318 4294993615 26322 4294993619
26824 26825 26828 26829 26832 26833
26826 4294994123 26830 4294994127 26834 4294994131

 

预处理对每一个4*4的矩阵要求为连续四个数,这样选每一个4*4的矩阵,他的上两行和下两行的异或值都相同,这样最后异或就是0 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

constexpr int N=1e5+7;
typedef long long ll;
int main(){
    int t;
    scanf("%d",&t);
    while(t--) {
        int n = 256, m = 256;

        int a[n][m];
        int cur = 0;
        for (int i = 0; i < n; i += 2) {
            for (int j = 0; j < m; j += 2) {
                a[i][j] = cur++;
                a[i][j + 1] = cur++;
                a[i + 1][j] = cur++;
                a[i + 1][j + 1] = cur++;
            }

        }
        /*for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }*/
        scanf("%d%d",&n,&m);
        printf("%d\n",n*m);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) printf("%d ",a[i][j]);
            printf("\n");
        }
    }
    return 0;
}

 

 

用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值