CF寒假补题集——Diverse Matrix

Diverse Matrix

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:

  1. b1b1 is the greatest common divisor of 22, 99, and 77, that is 11;
  2. b2b2 is the greatest common divisor of 44, 144144, and 8484, that is 44;
  3. b3b3 is the greatest common divisor of 22 and 44, that is 22;
  4. b4b4 is the greatest common divisor of 99 and 144144, that is 99;
  5. 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).

2 2

output

Copy

4 12
2 9

input

Copy

1 1

output

Copy

0

题目目标:找到行和列所有互异最大公因子中最大值中的最小值。

题目类型:模拟、

解题思路:找互异最大公因子,最小的排列为1~r+c;

若从1开始,则该行和该列的最大公因子都为1,因此至少从2开始;

分别讨论

行1, 列1, 的不可能0情况。

行1 ,列>1 的情况。

行>1, 列1的情况。

行>1, 列>1的情况。

int main()
{
     int r, c;
     cin>>r>>c;
     if(r == 1 && c == 1)
     {
         cout<<0<<endl;
         return 0;
     }
     int cnt = 2;
     if(c == 1)
     {
         for(int i = 1; i<= r; i++)
         {
             cout<<cnt++<<endl;
         }
         return 0;
     }
    for(int i = 1; i<= c; i++)
    {
        cout<<cnt++<<' ';
    }
    cout<<endl;
    for(int i = c+2; i<= c+r; i++)
    {
        for(int j = 2; j<=c+1; j++)
        {
            cout<<i*j<<' ';
        }
        cout<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值