C. Diverse Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
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).
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.
首先在正整数范围内,连续一段数字的gcd一定是1
那么为了使我们最大值最小,我们可以考虑第一行全部构造成 2 3 4....r+1
接下来每一列构造 r+2,r+3....r+c
那么如何构造?显然r+2..r+c是大于第一行每一个数字的,一旦我们在第一行每一列的数字基础上乘上r+2....r+c,那么该行gcd势必是r+2...r+c
于是构造完成
特判1 1 是0就OK了,一行或者一列其实也不需要特判
- # include
- # include
- # include
- # include
- # include
- using namespace std;
- typedef long long int ll;
-
- int ans[550][550];
- bool book[10000];
- int main ()
- {
-
-
- int n,m;
-
- cin>>n>>m;
-
-
- for(int i=1;i<=m;i++)
- {
- ans[1][i]=i+1;
-
- book[i+1]=1;
-
-
- }
-
- int last=2;
-
- for(int i=2;i<=n;i++)
- {
- for(int j=last;;j++)
- {
- if(!book[j])
- {
- book[j]=1;
- last=j;
- break;
- }
- }
-
- for(int j=1;j<=m;j++)
- {
- ans[i][j]=ans[1][j]*(last);
-
-
- }
-
- }
-
-
- if(n==1&&m==1)
- {
- cout<<0<
-
- return 0;
- }
- else if(n==1)
- {
- for(int i=1;i<=m;i++)
- {
- cout<1<<" ";
- }
-
- return 0;
- }
- else if(m==1)
- {
- for(int i=1;i<=n;i++)
- {
- cout<1<
- }
-
- return 0;
- }
-
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- cout<
" "; - }
-
- cout<
- }
-
- return 0;
- }
-
相关阅读:
MYSQL存储引擎和索引
Java之IO转换流
PWM控制蜂鸣器
2022双十一有哪些数码好物值得入手?双11实用性强的数码装备推荐
无痛理解傅里叶变换
数据库事务及事务隔离级别
【深入浅出 Yarn 架构与实现】2-2 Yarn 基础库 - 底层通信库 RPC
LeetCode高频题55. 跳跃游戏
QML与C++ 交互详解
html、css、js和jQuery学习总结
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126306769