B - Diverse Matrix

CodeForces – 1266C

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).ExamplesInput

2 2

Output

4 12
2 9

Input

1 1

Output

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.


思路:扩展性构造。贪心地去想对每一行每一列都是1~r+c的最大公因数

让1~r行的最大公因数分别是1~r,让1~c列的最大公因数是r+1~r+1+c;

我们构造的序列正常思考下是每行从左到右,每一列从上到下都是递增的。

那么此时a[1][1]应该放r+1,第一行的递增的每个数要互质才能让第一行是1,那么最方便的构造就是乘上递增的连续的数。方便点构造就是发现第i行时候j是不断连续递增的,满足互质。

这样就可以得出a[i][j]放i*(r+j);

另外特判掉r=1&&j==1 —0和r==1—–构造从2开始的递增,c==1同理

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=600;
typedef long long LL;
LL a[maxn][maxn]; 
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL r,c;cin>>r>>c;
  if(r==1&&c==1) 
  {
  	cout<<0<<endl;return 0;
  }
  else if(r==1||c==1)
  {
  	 if(r==1)
  	 {
  	 	for(LL j=1;j<=c;j++) a[1][j]=j+1;	
	 }
	 else 
	 {
	 	for(LL i=1;i<=r;i++) a[i][1]=i+1;
	 }
  }
  else
  {
  	 for(LL i=1;i<=r;i++)
  	 	for(LL j=1;j<=c;j++)
  	 	{
  	 		a[i][j]=i*(j+r);	
		}
  }
  for(LL i=1;i<=r;i++)
  {
  	for(LL j=1;j<=c;j++)
  		cout<<a[i][j]<<" ";
  		cout<<endl;
  }		
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值