GESP C++ Level 4 Real Exam Questions (2D Arrays) [202503] Second Order Matrix (luogu-B4264)

GESP C++ Level 4 real exam questions from March 2025. This question mainly tests the application of 2D arrays. It is considered a simple question among level 4 questions. Difficulty ⭐⭐☆☆☆. This question is rated as <span>beginner</span> by Luogu.

  • GESP Level 1 Practice Questions List

  • GESP Level 1 Real Exam Questions List

  • GESP Level 2 Practice Questions List

  • GESP Level 2 Real Exam Questions List

  • GESP Level 3 Practice Questions List

  • GESP Level 3 Real Exam Questions List

  • GESP Level 4 Practice Questions List

  • GESP Level 4 Real Exam Questions List

  • GESP Level 1-5 Syllabus Analysis

luogu-B4264 [GESP202503 Level 4] Second Order Matrix

Problem Requirements

Problem Description

Little A has a row column matrix .

Little A considers a matrix to be good if and only if . Here, represents the element in the row and column of the matrix.

Little A wants to know how many good submatrices are in .

Input Format

The first line contains two positive integers .

Next, lines, each containing integers .

Output Format

One line, an integer representing the number of good submatrices in .

Input Output Example #1

Input #1
3 4
1 2 1 0
2 4 2 1
0 3 3 0
Output #1
2

Explanation/Hint

Example Explanation

The good submatrices in the example are as follows:

GESP C++ Level 4 Real Exam Questions (2D Arrays) [202503] Second Order Matrix (luogu-B4264)
luogu-b4264
Data Range

For all test points, it is guaranteed that ,,

Problem Analysis

The core of this problem is to find all submatrices that meet the conditions in a matrix.

1. Data Structure Design

  • Use a 2D array <span>num_ary[505][505]</span> to store the input matrix
  • The array size is set to 505 to meet the maximum range requirement of the problem ()

2. Core Algorithm Idea

  • Traverse all possible top-left positions of 2×2 submatrices
  • Row traversal range:<span>[0, n-2]</span>, ensuring enough space to accommodate a 2×2 matrix
  • Column traversal range:<span>[0, m-2]</span>, similarly
  • Check:
    • Main diagonal:<span>num_ary[i][j]</span> and <span>num_ary[i+1][j+1]</span>
    • Anti-diagonal:<span>num_ary[i][j+1]</span> and <span>num_ary[i+1][j]</span>
    • Condition satisfied:<span>num_ary[i][j] * num_ary[i+1][j+1] == num_ary[i][j+1] * num_ary[i+1][j]</span>

3. Algorithm Complexity Analysis

  1. Time Complexity

  • Traverse all possible top-left positions of submatrices:
  • Judgment operation for each position:
  • Overall time complexity:
  • Space Complexity

    • Store the input matrix:
    • Other variables:
    • Overall space complexity:

    Example Code

    #include <iostream>
    
    // Define a 2D array to store matrix elements, supporting a maximum of 500x500 matrix
    int num_ary[505][505];
    
    int main() {
        // Define the number of rows n and columns m of the matrix
        int n, m;
        // Read the number of rows and columns of the matrix
        std::cin >> n >> m;
    
        // Read all elements of the matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                std::cin >> num_ary[i][j];
            }
        }
    
        // Counter to count the number of 2x2 submatrices that meet the conditions
        int count = 0;
        
        // Traverse all possible top-left positions of 2x2 submatrices
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < m - 1; j++) {
                // Check if the current 2x2 submatrix meets the condition: product of main diagonal elements equals product of anti-diagonal elements
                if (num_ary[i][j] * num_ary[i+1][j+1] == num_ary[i][j+1] * num_ary[i+1][j]) {
                    count++;
                }
            }
        }
    
        // Output the result
        std::cout << count;
        return 0;
    }
    

    For detailed GESP syllabus, real exam question explanations, knowledge expansion, and practice lists, see:

    [Pinned] GESP C++ Certification Learning Resources Summary

    luogu-” series questions can be evaluated online in the Luogu Question Bank.

    bcqm-” series questions can be evaluated online in the Programming Enlightenment Question Bank.

    Leave a Comment