Diverse Matrix
Let a be a matrix of size r×c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 1 to r, columns are numbered from 1 to c. We can construct an array b consisting of r+c integers as follows: for each i∈[1,r], let bi be the greatest common divisor of integers in the i-th row, and for each j∈[1,c] let br+j be the greatest common divisor of integers in the j-th column.
We call the matrix diverse if all r+c numbers bk (k∈[1,r+c]) are pairwise distinct.
The magnitude of a matrix equals to the maximum of bk.
For example, suppose we have the following matrix:
(249144784)
We construct the array b:
b1 is the greatest common divisor of 2, 9, and 7, that is 1;
b2 is the greatest common divisor of 4, 144, and 84, that is 4;
b3 is the greatest common divisor of 2 and 4, that is 2;
b4 is the greatest common divisor of 9 and 144, that is 9;
b5 is the greatest common divisor of 7 and 84, that is 7.
So b=[1,4,2,9,7]. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to 9.
For a given r and c, 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 0.
Input
The only line in the input contains two space separated integers r and c (1≤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 0.
Otherwise, output r rows. The i-th of them should contain c space-separated integers, the j-th of which is ai,j — the positive integer in the i-th row and j-th column of a diverse matrix minimizing the magnitude.
Furthermore, it must hold that 1≤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).
数学+思维题;
当r=1或c=1时,只要输出 2 开始到 r+c 的连续数字就行;
当不等于时,可以第一行第一列输出 c+1,第二行第一列输出 c+2, 以此类推;
那第几列输出对应得同行的第一列的倍数,这也就是这一列的最大公约数;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=998244353;
int main(){
ios::sync_with_stdio(false);
int r,c;
cin>>r>>c;
if(r==1&&c==1){
cout<<0<<endl;
return 0;
}
if(c==1||r==1){
if(c==1){
for(int i=1;i<=r;i++) cout<<i+1<<endl;
}
else if(r==1){
for(int i=1;i<=c;i++) cout<<i+1<<" ";
}
return 0;
}
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(j==1) cout<<i+c<<" ";
else cout<<(i+c)*j<<" ";
}
cout<<endl;
}
return 0;
}