Codeforces Global Round 6 :C. Diverse Matrix 《使数组的每行每列的gcd不同》

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 (1r,c5001≤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 1ai,j1091≤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.

 

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 50000+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
ll n,m,k,t,mx,mn,l,r,dp[mxn];
string str;

int main()
{
    TLE;
    while(cin>>n>>m)
    {
        if(n==1 && m==1)
            cout<<0<<endl;
        else
        {
            if(n>=m)
            {
                for(int i=1;i<=n;i++)
                {
                    for(int j=1;j<=m;j++)
                        cout<<(i+m)*j<<" ";
                    cout<<endl;
                }
            }
            else
                for(int i=1;i<=n;i++)
                {
                    for(int j=1;j<=m;j++)
                        cout<<(j+n)*i<<" ";
                    cout<<endl;
                }
        }
    }
    return 0;
}

 

posted @ 2019-12-18 09:37  __MEET  阅读(281)  评论(0编辑  收藏  举报