📜  查找通过将同一矩阵的行优先级和列优先级相加而形成的矩阵的痕迹

📅  最后修改于: 2021-04-27 18:12:22             🧑  作者: Mango

给定两个整数NM。考虑两个矩阵A NXM ,B NXM矩阵A和矩阵B都包含从1到N * M的元素。矩阵A包含以行为主的元素,矩阵B包含以列为主的元素。任务是找到由A和B相加形成的矩阵的轨迹。矩阵P NXM的轨迹定义为P [0] [0] + P [1] [1] + P [2] [2] + ….. + P [min(n – 1,m – 1)] [min(n – 1,m – 1)],即主对角线的加法。
注–矩阵A和矩阵B都包含从1到N * M的元素。

例子 :

Input : N = 3, M = 3
Output : 30
Therefore,
    1 2 3
A = 4 5 6
    7 8 9

    1 4 7
B = 2 5 8
    3 6 9
  
        2 6 10
A + B = 6 10 14
       10 14 18

Trace = 2 + 10 + 18 = 30

方法1(天真的方法):
生成矩阵A和B并找到总和。然后遍历主要诊断并找到总和。

以下是此方法的实现:

C++
// C++ program to find
// trace of matrix formed by
// adding Row-major and
// Column-major order of same matrix
#include 
using namespace std;
 
// Return the trace of
// sum of row-major matrix
// and column-major matrix
int trace(int n, int m)
{
 
    int A[n][m], B[n][m], C[n][m];   
 
    // Generating the matrix A
    int cnt = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            A[i][j] = cnt;
            cnt++;
        }   
 
    // Generating the matrix A
    cnt = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            B[j][i] = cnt;
            cnt++;
        }
 
    // Finding sum of matrix A and matrix B
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            C[i][j] = A[i][j] + B[i][j];   
 
    // Finding the trace of matrix C.
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (i == j)
                sum += C[i][j];
 
    return sum;
}
 
// Driven Program
int main()
{
    int N = 3, M = 3;
    cout << trace(N, M) << endl;
    return 0;
}


Java
// Java program to find
// trace of matrix formed by
// adding Row-major and
// Column-major order of same matrix
class GFG
{
    // Return the trace of
    // sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
     
        int A[][] = new int[n][m];
        int B[][] = new int[n][m];
        int C[][] = new int[n][m];
     
        // Generating the matrix A
        int cnt = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                A[i][j] = cnt;
                cnt++;
            }
     
        // Generating the matrix A
        cnt = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                B[j][i] = cnt;
                cnt++;
            }
     
        // Finding sum of matrix A and matrix B
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                C[i][j] = A[i][j] + B[i][j];
     
        // Finding the trace of matrix C.
        int sum = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (i == j)
                    sum += C[i][j];
     
        return sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 3, M = 3;
         
        System.out.println(trace(N, M));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find trace of matrix
# formed by adding Row-major and
# Column-major order of same matrix
 
# Return the trace of sum of row-major
# matrix and column-major matrix
def trace(n, m):
 
    A = [[0 for x in range(m)]
            for y in range(n)];
    B = [[0 for x in range(m)]
            for y in range(n)];
    C = [[0 for x in range(m)]
            for y in range(n)];
 
    # Generating the matrix A
    cnt = 1;
    for i in range(n):
        for j in range(m):
            A[i][j] = cnt;
            cnt += 1;
 
    # Generating the matrix A
    cnt = 1;
    for i in range(n):
        for j in range(m):
            B[j][i] = cnt;
            cnt += 1;
 
    # Finding sum of matrix A and matrix B
    for i in range(n):
        for j in range(m):
            C[i][j] = A[i][j] + B[i][j];
 
    # Finding the trace of matrix C.
    sum = 0;
    for i in range(n):
        for j in range(m):
            if (i == j):
                sum += C[i][j];
 
    return sum;
 
# Driver Code
N = 3;
M = 3;
print(trace(N, M));
     
# This code is contributed by mits


C#
// C# program to find
// trace of matrix formed by
// adding Row-major and
// Column-major order of same matrix
using System;
 
class GFG {
     
    // Return the trace of
    // sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
        int[, ] A = new int[n, m];
        int[, ] B = new int[n, m];
        int[, ] C = new int[n, m];
 
        // Generating the matrix A
        int cnt = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                A[i, j] = cnt;
                cnt++;
            }
 
        // Generating the matrix A
        cnt = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                B[j, i] = cnt;
                cnt++;
            }
 
        // Finding sum of matrix A and matrix B
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                C[i, j] = A[i, j] + B[i, j];
 
        // Finding the trace of matrix C.
        int sum = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (i == j)
                    sum += C[i, j];
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 3, M = 3;
        Console.WriteLine(trace(N, M));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
#include 
using namespace std;
 
// Return sum of first n integers of an AP
int sn(int n, int an)
{
    return (n * (1 + an)) / 2;
}
 
// Return the trace of sum of row-major matrix
// and column-major matrix
int trace(int n, int m)
{
    // Finding nth element in
    // AP in case of Row major matrix.
    int an = 1 + (n - 1) * (m + 1);
 
    // Finding sum of first n integers
    // of AP in case of Row major matrix
    int rowmajorSum = sn(n, an);
 
    // Finding nth element in AP
    // in case of Row major matrix
    an = 1 + (n - 1) * (n + 1);
 
    // Finding sum of first n integers
    // of AP in case of Column major matrix
    int colmajorSum = sn(n, an);
 
    return rowmajorSum + colmajorSum;
}
 
// Driven Program
int main()
{
    int N = 3, M = 3;
    cout << trace(N, M) << endl;
    return 0;
}


Java
// Java program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
import java.io.*;
 
public class GFG {
 
    // Return sum of first n integers of an AP
    static int sn(int n, int an)
    {
        return (n * (1 + an)) / 2;
    }
 
    // Return the trace of sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
        // Finding nth element in
        // AP in case of Row major matrix.
        int an = 1 + (n - 1) * (m + 1);
 
        // Finding sum of first n integers
        // of AP in case of Row major matrix
        int rowmajorSum = sn(n, an);
 
        // Finding nth element in AP
        // in case of Row major matrix
        an = 1 + (n - 1) * (n + 1);
 
        // Finding sum of first n integers
        // of AP in case of Column major matrix
        int colmajorSum = sn(n, an);
 
        return rowmajorSum + colmajorSum;
    }
 
    // Driven Program
    static public void main(String[] args)
    {
        int N = 3, M = 3;
        System.out.println(trace(N, M));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to find trace
# of matrix formed by adding
# Row-major and Column-major
# order of same matrix
 
# Return sum of first n
# integers of an AP
def sn(n, an):
    return (n * (1 + an)) / 2;
 
# Return the trace of sum
# of row-major matrix
# and column-major matrix
def trace(n, m):
     
    # Finding nth element
    # in AP in case of
    # Row major matrix.
    an = 1 + (n - 1) * (m + 1);
     
    # Finding sum of first
    # n integers of AP in
    # case of Row major matrix
    rowmajorSum = sn(n, an);
     
    # Finding nth element in AP
    # in case of Row major matrix
    an = 1 + (n - 1) * (n + 1);
     
    # Finding sum of first n
    # integers of AP in case
    # of Column major matrix
    colmajorSum = sn(n, an);
     
    return int(rowmajorSum +
               colmajorSum);
     
# Driver Code
N = 3;
M = 3;
print(trace(N, M));
 
# This code is contributed mits


C#
// C# program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
using System;
 
public class GFG {
 
    // Return sum of first n integers of an AP
    static int sn(int n, int an)
    {
        return (n * (1 + an)) / 2;
    }
 
    // Return the trace of sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
        // Finding nth element in
        // AP in case of Row major matrix.
        int an = 1 + (n - 1) * (m + 1);
 
        // Finding sum of first n integers
        // of AP in case of Row major matrix
        int rowmajorSum = sn(n, an);
 
        // Finding nth element in AP
        // in case of Row major matrix
        an = 1 + (n - 1) * (n + 1);
 
        // Finding sum of first n integers
        // of AP in case of Column major matrix
        int colmajorSum = sn(n, an);
 
        return rowmajorSum + colmajorSum;
    }
 
    // Driven Program
    static public void Main()
    {
        int N = 3, M = 3;
        Console.WriteLine(trace(N, M));
    }
}
 
// This code is contributed by vt_m.


PHP


输出 :

30

时间复杂度: O(N * M)。

方法2(有效方法):
基本上,我们需要找到第一个矩阵A的主对角线和第二个矩阵B的主对角线之和。
让我们举个例子,N = 3,M = 4。
因此,行主矩阵将是

1  2  3  4
A =  5  6  7  8
     9 10 11 12

因此,我们需要1、6、11的总和。
观察,它形成一个具有列数M恒定差的算术级数。
同样,第一个元素始终为1。因此,在行占主导的矩阵的情况下形成的AP为1、1+(M + 1),1 + 2 *(M + 1),..由N(行数)组成)元素。我们知道,
S n =(n *(a 1 + a n ))/ 2
因此,n = R,a 1 = 1, n = 1 +(R – 1)*(M + 1)。
类似地,在以“主要列”为例的情况下,形成的AP将为1、1+(N + 1),1 + 2 *(N + 1)…..
所以,N = R,A 1 = 1,n = 1+(R – 1)*(N + 1)。

以下是此方法的实现:

C++

// C++ program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
#include 
using namespace std;
 
// Return sum of first n integers of an AP
int sn(int n, int an)
{
    return (n * (1 + an)) / 2;
}
 
// Return the trace of sum of row-major matrix
// and column-major matrix
int trace(int n, int m)
{
    // Finding nth element in
    // AP in case of Row major matrix.
    int an = 1 + (n - 1) * (m + 1);
 
    // Finding sum of first n integers
    // of AP in case of Row major matrix
    int rowmajorSum = sn(n, an);
 
    // Finding nth element in AP
    // in case of Row major matrix
    an = 1 + (n - 1) * (n + 1);
 
    // Finding sum of first n integers
    // of AP in case of Column major matrix
    int colmajorSum = sn(n, an);
 
    return rowmajorSum + colmajorSum;
}
 
// Driven Program
int main()
{
    int N = 3, M = 3;
    cout << trace(N, M) << endl;
    return 0;
}

Java

// Java program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
import java.io.*;
 
public class GFG {
 
    // Return sum of first n integers of an AP
    static int sn(int n, int an)
    {
        return (n * (1 + an)) / 2;
    }
 
    // Return the trace of sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
        // Finding nth element in
        // AP in case of Row major matrix.
        int an = 1 + (n - 1) * (m + 1);
 
        // Finding sum of first n integers
        // of AP in case of Row major matrix
        int rowmajorSum = sn(n, an);
 
        // Finding nth element in AP
        // in case of Row major matrix
        an = 1 + (n - 1) * (n + 1);
 
        // Finding sum of first n integers
        // of AP in case of Column major matrix
        int colmajorSum = sn(n, an);
 
        return rowmajorSum + colmajorSum;
    }
 
    // Driven Program
    static public void main(String[] args)
    {
        int N = 3, M = 3;
        System.out.println(trace(N, M));
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to find trace
# of matrix formed by adding
# Row-major and Column-major
# order of same matrix
 
# Return sum of first n
# integers of an AP
def sn(n, an):
    return (n * (1 + an)) / 2;
 
# Return the trace of sum
# of row-major matrix
# and column-major matrix
def trace(n, m):
     
    # Finding nth element
    # in AP in case of
    # Row major matrix.
    an = 1 + (n - 1) * (m + 1);
     
    # Finding sum of first
    # n integers of AP in
    # case of Row major matrix
    rowmajorSum = sn(n, an);
     
    # Finding nth element in AP
    # in case of Row major matrix
    an = 1 + (n - 1) * (n + 1);
     
    # Finding sum of first n
    # integers of AP in case
    # of Column major matrix
    colmajorSum = sn(n, an);
     
    return int(rowmajorSum +
               colmajorSum);
     
# Driver Code
N = 3;
M = 3;
print(trace(N, M));
 
# This code is contributed mits

C#

// C# program to find trace of matrix formed
// by adding Row-major and Column-major order
// of same matrix
using System;
 
public class GFG {
 
    // Return sum of first n integers of an AP
    static int sn(int n, int an)
    {
        return (n * (1 + an)) / 2;
    }
 
    // Return the trace of sum of row-major matrix
    // and column-major matrix
    static int trace(int n, int m)
    {
        // Finding nth element in
        // AP in case of Row major matrix.
        int an = 1 + (n - 1) * (m + 1);
 
        // Finding sum of first n integers
        // of AP in case of Row major matrix
        int rowmajorSum = sn(n, an);
 
        // Finding nth element in AP
        // in case of Row major matrix
        an = 1 + (n - 1) * (n + 1);
 
        // Finding sum of first n integers
        // of AP in case of Column major matrix
        int colmajorSum = sn(n, an);
 
        return rowmajorSum + colmajorSum;
    }
 
    // Driven Program
    static public void Main()
    {
        int N = 3, M = 3;
        Console.WriteLine(trace(N, M));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出 :

30