C. Diverse Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let aa be a matrix of size r×cr×c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 11 to rr, columns are numbered from 11 to cc. We can construct an array bb consisting of r+cr+c integers as follows: for each i∈[1,r]i∈[1,r], let bibi be the greatest common divisor of integers in the ii-th row, and for each j∈[1,c]j∈[1,c] let br+jbr+j be the greatest common divisor of integers in the jj-th column.
We call the matrix diverse if all r+cr+c numbers bkbk (k∈[1,r+c]k∈[1,r+c]) are pairwise distinct.
The magnitude of a matrix equals to the maximum of bkbk.
For example, suppose we have the following matrix:
(249144784)(297414484)
We construct the array bb:
- b1b1 is the greatest common divisor of 22, 99, and 77, that is 11;
- b2b2 is the greatest common divisor of 44, 144144, and 8484, that is 44;
- b3b3 is the greatest common divisor of 22 and 44, that is 22;
- b4b4 is the greatest common divisor of 99 and 144144, that is 99;
- b5b5 is the greatest common divisor of 77 and 8484, that is 77.
So b=[1,4,2,9,7]b=[1,4,2,9,7]. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to 99.
For a given rr and cc, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer 00.
Input
The only line in the input contains two space separated integers rr and cc (1≤r,c≤5001≤r,c≤500) — the number of rows and the number of columns of the matrix to be found.
Output
If there is no solution, output a single integer 00.
Otherwise, output rr rows. The ii-th of them should contain cc space-separated integers, the jj-th of which is ai,jai,j — the positive integer in the ii-th row and jj-th column of a diverse matrix minimizing the magnitude.
Furthermore, it must hold that 1≤ai,j≤1091≤ai,j≤109. It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude).
Examples
input
Copy
2 2
output
Copy
4 12 2 9
input
Copy
1 1
output
Copy
0
Note
In the first example, the GCDs of rows are b1=4b1=4 and b2=1b2=1, and the GCDs of columns are b3=2b3=2 and b4=3b4=3. All GCDs are pairwise distinct and the maximum of them is 44. Since the GCDs have to be distinct and at least 11, it is clear that there are no diverse matrices of size 2×22×2 with magnitude smaller than 44.
In the second example, no matter what a1,1a1,1 is, b1=b2b1=b2 will always hold, so there are no diverse matrices.
首先在正整数范围内,连续一段数字的gcd一定是1
那么为了使我们最大值最小,我们可以考虑第一行全部构造成 2 3 4....r+1
接下来每一列构造 r+2,r+3....r+c
那么如何构造?显然r+2..r+c是大于第一行每一个数字的,一旦我们在第一行每一列的数字基础上乘上r+2....r+c,那么该行gcd势必是r+2...r+c
于是构造完成
特判1 1 是0就OK了,一行或者一列其实也不需要特判
# include<iostream>
# include<vector>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;
typedef long long int ll;
int ans[550][550];
bool book[10000];
int main ()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
ans[1][i]=i+1;
book[i+1]=1;
}
int last=2;
for(int i=2;i<=n;i++)
{
for(int j=last;;j++)
{
if(!book[j])
{
book[j]=1;
last=j;
break;
}
}
for(int j=1;j<=m;j++)
{
ans[i][j]=ans[1][j]*(last);
}
}
if(n==1&&m==1)
{
cout<<0<<endl;
return 0;
}
else if(n==1)
{
for(int i=1;i<=m;i++)
{
cout<<i+1<<" ";
}
return 0;
}
else if(m==1)
{
for(int i=1;i<=n;i++)
{
cout<<i+1<<endl;
}
return 0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}