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

Examples
inputCopy

2 2
outputCopy
4 12
2 9
inputCopy
1 1
outputCopy
0
Note
In the first example, the GCDs of rows are b1=4 and b2=1, and the GCDs of columns are b3=2 and b4=3. All GCDs are pairwise distinct and the maximum of them is 4. Since the GCDs have to be distinct and at least 1, it is clear that there are no diverse matrices of size 2×2 with magnitude smaller than 4.

In the second example, no matter what a1,1 is, b1=b2 will always hold, so there are no diverse matrices.

题意:给你r和c,让你构造出来一个 r∗c的矩阵,矩阵元素每一行的最大公约数和每一列的最大公约数不能相同,让你构造出来一个最大公约数最小的矩阵。

解析:每一行每一列的最大公约数肯定在1–r+c范围内这样才是最优的。
第一行最大公约数为1,a[0][0]填2。不能填1,填1会使第一列的最大公约数为1
就重复了。那么第一行就是2,3,4,5…c+1,那么自然的每列的最大公约数也是2,3,4,5…c+1.
那么每行的最大公约数为 c+2-----r+c 。那么每行的数=该行所在的最大公约数*第一行的该列的数。
上面讨论的使r<c;
如果是r>c,只要swap(r,c),最后再换回来。

具体看代码


#include<bits/stdc++.h>
using namespace std;
 
long long a[505][505];
long long b[505][505];
int flag = 0;
int main(){
    int r,c;
    cin>>r>>c;
    if(r>c){
        swap(r,c);
        flag = 1;
    }
    if(r==1&&c==1){
        cout<<"0"<<endl;
        return 0;
    }
    for(int i=0;i<c;i++){
        a[0][i]=i+2;
    }
    for(int i=1;i<r;i++){
        for(int j=0;j<c;j++){
            a[i][j]=(1+c+i)*a[0][j];
        }
    }
    if(flag == 1){
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                b[j][i]=a[i][j];
            }
        }
        swap(r,c);
    }
 
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(flag==1)
                cout<<b[i][j]<<" ";
                else
                    cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值