• C. Diverse Matrix


    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:

    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 (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了,一行或者一列其实也不需要特判

    1. # include
    2. # include
    3. # include
    4. # include
    5. # include
    6. using namespace std;
    7. typedef long long int ll;
    8. int ans[550][550];
    9. bool book[10000];
    10. int main ()
    11. {
    12. int n,m;
    13. cin>>n>>m;
    14. for(int i=1;i<=m;i++)
    15. {
    16. ans[1][i]=i+1;
    17. book[i+1]=1;
    18. }
    19. int last=2;
    20. for(int i=2;i<=n;i++)
    21. {
    22. for(int j=last;;j++)
    23. {
    24. if(!book[j])
    25. {
    26. book[j]=1;
    27. last=j;
    28. break;
    29. }
    30. }
    31. for(int j=1;j<=m;j++)
    32. {
    33. ans[i][j]=ans[1][j]*(last);
    34. }
    35. }
    36. if(n==1&&m==1)
    37. {
    38. cout<<0<
    39. return 0;
    40. }
    41. else if(n==1)
    42. {
    43. for(int i=1;i<=m;i++)
    44. {
    45. cout<1<<" ";
    46. }
    47. return 0;
    48. }
    49. else if(m==1)
    50. {
    51. for(int i=1;i<=n;i++)
    52. {
    53. cout<1<
    54. }
    55. return 0;
    56. }
    57. for(int i=1;i<=n;i++)
    58. {
    59. for(int j=1;j<=m;j++)
    60. {
    61. cout<" ";
    62. }
    63. cout<
    64. }
    65. return 0;
    66. }

  • 相关阅读:
    【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