第一天 B. Diverse Matrix

11 篇文章 0 订阅
3 篇文章 0 订阅

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

思路如下:要求寻找每一橫行和纵行公共的最大公因数,要求使最大的最小,那么根据贪心的思路,使最大公因数为从1 到 r + c 即可。所以每一橫行从1开始到r标记,每一纵行从r + 1 到 r + c 标记,每一个坐标就是橫行标记乘以纵行标记。特判r = 1, c = 1, r = c = 1三种特例

代码如下:

int r, c;
    cin >> r >> c;

    //初始化
    rep(i, 1, r){
        rep(j, 1, c){
            ans[i][j] = 1;
        }
    }

    //r == c == 1
    if(r == c && r == 1){
        cout << 0;
        return 0;
    }

    //r == 1
    if(r == 1){
        rep(i, 1, c){
            cout << i + 1 << " ";
        }
        return 0;
    }

    //c == 1
    if(c == 1){
        rep(i, 1, r){
            cout << i + 1 << endl;
        }
        return 0;
    }

    //一般情况
    int cnt =  1, k = 1;
    rep(i, 1, r){
        rep(j, 1, c){
            ans[i][j] *= cnt;
        }
        cnt ++ ;
    }

    rep(i, 1, c){
        rep(j, 1, r){
            ans[j][i] *= cnt;
        }
        cnt ++ ;
    }

    rep(i, 1, r){
        rep(j, 1, c){
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值