C. The Very Beautiful Blanket
C. The Very Beautiful Blanket
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 of the matrix is true:
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 () — the number of test cases.
The single line of each test case contains two integers and — the size of matrix .
It is guaranteed that the sum of does not exceed .
Output
For each test case, in first line output one integer — the maximum number of different numbers in the matrix.
Then output the matrix 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 .
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 . Consider a submatrix whose upper-left corner coincides with the upper-left corner of the matrix :
;
.
So, chosen submatrix fits the condition. Similarly, you can make sure that the other three submatrices also fit the condition.
解题思路
考虑让每个的子矩阵的异或和为,那么对于任意一个的矩阵对应的异或和都满足题目的要求。构造的方法是对于坐标,直接令就可以了。这是因为,相当于让横坐标左移位,底位用来表示纵坐标。那么对于一个的子矩阵,分别考虑低位和剩余的位,经过验算有,即这种构造方法可以使得任意一个的子矩阵的异或和为。很明显任意一个的值都不同,因此不同的数有个。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17217276.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效