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;
- }
-
相关阅读:
【MATLAB教程案例4】直接序列扩频通信系统的MATLAB仿真
查看/清除Windows系统SID
1003 Emergency
OpenGL ES glad 下载和使用
360杀毒卸载办法
【数据结构与算法】之“堆”介绍
【算法 | 实验18】在字符矩阵中查找给定字符串的所有匹配项
计算机组成原理之加密技术和认证技术(软考中级)
循环结构( for循环、双重for循环)
PMP每日一练 | 考试不迷路-8.10(包含敏捷+多选)
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126306769
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU