C. The Very Beautiful Blanket

C. The Very Beautiful Blanket

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:

  • A11A12A21A22=A33A34A43A44,
  • A13A14A23A24=A31A32A41A42,

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 (1t1000) — the number of test cases.

The single line of each test case contains two integers n and m (4n,m200) — the size of matrix B.

It is guaranteed that the sum of nm does not exceed 2105.

Output

For each test case, in first line output one integer cnt (1cntnm) — the maximum number of different numbers in the matrix.

Then output the matrix B (0Bij<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 (0Bij<263).

Example

input

4
5 5
4 4
4 6
6 6

output

复制代码
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
复制代码

Note

In the first test case, there is only 4 submatrix of size 4×4. Consider a submatrix whose upper-left corner coincides with the upper-left corner of the matrix B:

[974015499744155315501551155415551025220611025620652062206320662067]

9740154915501551 = 10256206520662067 = 8192;

10252206120622063 = 9744155315541555 = 8192.

So, chosen submatrix fits the condition. Similarly, you can make sure that the other three submatrices also fit the condition.

 

解题思路

  考虑让每个2×2的子矩阵的异或和为0,那么对于任意一个4×4的矩阵对应的异或和都满足题目的要求。构造的方法是对于坐标(i,j),直接令ai,j=i×28+j就可以了。这是因为m200<28,相当于让横坐标左移8位,底8位用来表示纵坐标。那么对于一个2×2的子矩阵,分别考虑低8位和剩余的位,经过验算有ai,jai,j+1ai+1,jai+1,j+1=0,即这种构造方法可以使得任意一个2×2的子矩阵的异或和为0。很明显任意一个ai,j的值都不同,因此不同的数有n×m个。

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void solve() {
 5     int n, m;
 6     scanf("%d %d", &n, &m);
 7     printf("%d\n", n * m);
 8     for (int i = 0; i < n; i++) {
 9         for (int j = 0; j < m; j++) {
10             printf("%d ", (i << 8) + j);
11         }
12         printf("\n");
13     }
14 }
15 
16 int main() {
17     int t;
18     scanf("%d", &t);
19     while (t--) {
20         solve();
21     }
22     
23     return 0;
24 }
复制代码

 

参考资料

  [Codeforces Round 857 (Div. 1)][Codeforces 1801A~1801G(部分,已更新A~D、F)]:https://www.cnblogs.com/DeaphetS/p/17202190.html

posted @   onlyblues  阅读(115)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示