GCD to 1 (Easy) This is the easy version of the problem. The only difference between…
- User
GCD to 1 (Easy) This is the easy version of the problem. The only difference between this and hard version is that you don't need to minimize the sum of matrix in this version
Construct a matrix A A of size N ∗ M N∗M such that the GCD of every row and column is 1 1 and element must be 2 <
A i , j <
1 0 9 2<=A i,j <=10 9 .
If there are multiple answers possible, print any.
Input Format The first line of input will contain a single integer T T, denoting the number of test cases. Each test case consists of single line of input. The first line of each test case contains two space-separated integers N N and M M — the number of rows and columns in the matrix, respectively. Output Format For each test case, print a matrix of size N ∗ M N∗M.
Constraints 1 ≤ T ≤ 1 0 4 1≤T≤10 4
2 ≤ N , M ≤ 3 ⋅ 1 0 5 2≤N,M≤3⋅10 5
The sum of N ⋅ M N⋅M over all test cases won't exceed 3 ⋅ 1 0 5 3⋅10 5 . Sample 1: Input Output 3 2 3 3 3 4 2 9 7 8 4 10 13 5 2 9 6 11 7 19 15 16 20 17 33 7 21 8 13 3 Explanation: In the first case, there are 2 rows and 3 columns
GCD of 1st row = GCD(9,7,8) = 1 GCD of 2nd row = GCD(4,10,13) = 1 GCD of 1st column = GCD(9,4) = 1 GCD of 2nd column = GCD(7,10) = 1 GCD of 3rd column = GCD(8,13) = 1
- Assistant
To solve this problem, we need to construct a matrix where every row and column has a Greatest Common Divisor (GCD) of 1, and each element is within the specified range (2 ≤ A[i][j] ≤ 1e9).
An effective approach is to assign values to the matrix in such a way that consecutive elements in any row or column differ by 1. This ensures that the GCD of those elements is 1 because the GCD of any two consecutive integers is 1.
Here's how we can do it:
- Assign each element A[i][j] = i + j
This assignment ensures that:
- Each element is at least 2 (since i and j start from 1).
- Each element does not exceed 2 * max(N, M) (which is within 1e9 given the constraints).