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; }