Table of Contents
Example
Input
Output
Home Backend Development C++ C++ program to find the number of unique matrices that can be generated by swapping rows and columns

C++ program to find the number of unique matrices that can be generated by swapping rows and columns

Sep 01, 2023 am 11:53 AM
matrix exchange only

C++ program to find the number of unique matrices that can be generated by swapping rows and columns

Suppose we have an n x n matrix. Each element in the matrix is ​​unique and an integer between 1 and n2. Now we can perform the following operations in any number and in any order.

  • We choose any two integers x and y in the matrix where (1 ≤ x

  • We choose any two integers x and y in the matrix where (1 ≤ x

  • We must note that x y ≤ k and these values ​​cannot appear in the same row and column.

We have to find out the number of unique matrices that can be obtained by performing the operation.

So if the input is something like n = 3, k = 15, mat = {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}, then the output will be 36.

For example, the two values ​​selected are x = 3 and y = 5. If you swap the columns, the resulting matrix will be -

3 4 6
9 5 7
2 1 8
Copy after login

In this way you can get 36 such unique matrices.

To solve this problem we will follow the following steps-

Define a function dfs(), this will take k, arrays ver and visited, one stack s.
   if visited[k] is non-zero, then:
      return
   visited[k] := true
   insert k into s
   for initialize iterator j := start of ver[k], when j is not equal to last element of ver[k], update (increase j by 1), do:
      dfs(*j, ver, visited, s)
Define an array f of size: 51.
f[0] := 1
for initialize i := 1, when i <= 50, update (increase i by 1), do:
   f[i] := (i * f[i - 1]) mod modval
Define an array e of size n
Define an array pk of size n
for initialize i := 0, when i < n, update (increase i by 1), do:
   for initialize j := i + 1, when j < n, update (increase j by 1), do:
      chk := 0
         for initialize l := 0, when l < n, update (increase l by 1), do:
            if (mat[i, l] + mat[j, l]) > k, then:
               chk := 1
               Come out from the loop
         if chk is same as 0, then:
             insert j at the end of pk[i]
             insert i at the end of pk[j]
          chk := 0
          for initialize l := 0, when l < n, update (increase l by 1), do:
             if (mat[l, i] + mat[l, j]) > k, then:
                chk := 1
                Come out from the loop
           if chk is same as 0, then:
               insert j at the end of e[i]
               insert i at the end of e[j]
resa := 1, resb = 1
Define an array v1 of size: n and v2 of size: n.
for initialize i := 0, when i < n, update (increase i by 1), do:
   v1[i] := false
   v2[i] := false
for initialize i := 0, when i < n, update (increase i by 1), do:
   Define one stack s.
   if not v1[i] is non-zero, then:
      dfs(i, pk, v1, s)
      if not s is empty, then:
         resa := resa * (f[size of s])
         resa := resa mod modval
for initialize i := 0, when i < n, update (increase i by 1), do:
   Define one stack s
   if not v2[i] is non-zero, then:
      dfs(i, e, v2, s)
      if not s is empty, then:
         resb := resb * (f[size of s])
         resb := resb mod modval
print((resa * resb) mod modval)
Copy after login

Example

Let us see the following implementation for better understanding-

#include <bits/stdc++.h>
using namespace std;
#define modval 998244353
const int INF = 1e9;
void dfs(int k, vector<int> ver[], bool visited[], stack<int> &s) {
   if(visited[k])
      return;
   visited[k] = true;
   s.push(k);
   for(vector<int> :: iterator j = ver[k].begin(); j!=ver[k].end(); j++)
      dfs(*j, ver, visited, s);
}
void solve(int n, int k, vector<vector<int>> mat) {
   int f[51];
   f[0] = 1;
   for(int i = 1; i <= 50; i++) {
      f[i] = (i * f[i-1]) % modval;
   }
   vector<int> e[n];
   vector<int> pk[n];
   for(int i = 0; i < n; i++) {
      for(int j = i + 1;j < n; j++) {
         int chk = 0;
         for(int l = 0; l < n; l++){
            if((mat[i][l] + mat[j][l]) > k) {
               chk = 1;
               break;
            }
         }
         if(chk==0) {
            pk[i].push_back(j);
            pk[j].push_back(i);
         }
         chk = 0;
         for(int l = 0;l < n; l++) {
            if((mat[l][i] + mat[l][j]) > k){
               chk = 1;
               break;
            }
         }
         if(chk == 0) {
            e[i].push_back(j);
            e[j].push_back(i);
        }
      }
   }
   int resa = 1, resb = 1;
   bool v1[n], v2[n];
   for(int i = 0; i < n; i++) {
      v1[i] = false;
      v2[i] = false;
   }
   for(int i = 0;i < n; i++) {
      stack<int> s;
      if(!v1[i]) {
         dfs(i, pk, v1, s);
         if(!s.empty()) {
             resa *= (f[s.size()]) % modval;
             resa %= modval;
         }
      }
   }
   for(int i = 0 ;i < n; i++) {
      stack<int> s;
      if(!v2[i]){
         dfs(i, e, v2, s);
         if(!s.empty()) {
           resb *= (f[s.size()]) % modval;
            resb %= modval;
         }
      }
   }
   cout<< (resa * resb) % modval;
}
int main() {
   int n = 3, k = 15;
   vector<vector<int>> mat = {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}};
   solve(n, k, mat);
   return 0;
}
Copy after login

Input

3, 15, {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}
Copy after login

Output

36
Copy after login

The above is the detailed content of C++ program to find the number of unique matrices that can be generated by swapping rows and columns. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Nov 20, 2023 pm 05:25 PM

In the first article of this series, we discussed the connections and differences between artificial intelligence, machine learning, deep learning, data science, and more. We also made some hard choices about the programming languages, tools, and more that the entire series would use. Finally, we also introduced a little bit of matrix knowledge. In this article, we will discuss in depth the matrix, the core of artificial intelligence. But before that, let’s first understand the history of artificial intelligence. Why do we need to understand the history of artificial intelligence? There have been many AI booms in history, but in many cases the huge expectations for AI's potential failed to materialize. Understanding the history of artificial intelligence can help us see whether this wave of artificial intelligence will create miracles or is just another bubble about to burst. us

How to add swap space on Ubuntu 22.04 LTS How to add swap space on Ubuntu 22.04 LTS Feb 20, 2024 am 11:12 AM

Swap space plays an important role in Linux systems, especially when the system is low on memory. It acts as a backup memory storage space that helps the system run smoothly and maintain stability even under high load. This article provides you with a detailed guide to adding swap space on Ubuntu 22.04LTS to ensure that your system performance is optimized and can handle various workloads. Understanding Swap Space Swap space provides virtual memory that is used to supplement the system's physical RAM. When the system is low on RAM, the kernel swaps data to disk to prevent out-of-memory and system crashes. Linux systems commonly use swap space to handle this situation. Run multiple memory-intensive applications simultaneously to process very large files or data

Python program to calculate the sum of the right diagonal elements of a matrix Python program to calculate the sum of the right diagonal elements of a matrix Aug 19, 2023 am 11:29 AM

A popular general-purpose programming language is Python. It is used in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python has a simple and easy-to-understand syntax that is suitable for beginners. In this article, we will use Python to calculate the sum of the right diagonal of a matrix. What is a matrix? In mathematics, we use a rectangular array or matrix to describe a mathematical object or its properties. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows and columns. . For example -234512367574 Therefore, this is a matrix with 3 rows and 4 columns, expressed as a 3*4 matrix. Now, there are two diagonals in the matrix, the primary diagonal and the secondary diagonal

How to calculate the determinant of a matrix or ndArray using numpy in Python? How to calculate the determinant of a matrix or ndArray using numpy in Python? Aug 18, 2023 pm 11:57 PM

In this article, we will learn how to calculate the determinant of a matrix using the numpy library in Python. The determinant of a matrix is ​​a scalar value that can represent the matrix in compact form. It is a useful quantity in linear algebra and has numerous applications in various fields including physics, engineering, and computer science. In this article, we will first discuss the definition and properties of determinants. We will then learn how to use numpy to calculate the determinant of a matrix and see how it is used in practice through some examples. Thedeterminantofamatrixisascalarvaluethatcanbeusedtodescribethepropertie

Python program to multiply two matrices using multidimensional arrays Python program to multiply two matrices using multidimensional arrays Sep 11, 2023 pm 05:09 PM

A matrix is ​​a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an mXn matrix, and m and n are called its dimensions. A matrix is ​​a two-dimensional array created in Python using lists or NumPy arrays. In general, matrix multiplication can be done by multiplying the rows of the first matrix by the columns of the second matrix. Here, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Input and output scenario Suppose we have two matrices A and B. The dimensions of these two matrices are 2X3 and 3X2 respectively. The resulting matrix after multiplication will have 2 rows and 1 column. [b1,b2][a1,a2,a3]*[b3,b4]=[a1*b1+a2*b2+a3*a3][a4,a5,a6][b5,b6][a4*b2+a

C/C++ program to find the product of the unique prime factors of a number C/C++ program to find the product of the unique prime factors of a number Sep 18, 2023 am 10:01 AM

A unique prime factor is also a factor of a prime number. In this problem, we have to find the product of all unique prime factors of a number. A prime number is a number with only two factors, the number and one. Here we will try to find the best way to calculate the product of the unique prime factors of a number. number. Let's take an example to illustrate the problem more clearly. There is a number n=1092, and we must find the product of its unique prime factors. The prime factors of 1092 are 2,3,7,13 and the product is 546. 2A simple way to find this is to find all the factors of the number and check if the factor is a prime number. If it is then multiplied by a number then the multiplication variable is returned. Input:n=10Output:10 is explained here, the input

C program to compare two matrices for equality C program to compare two matrices for equality Aug 31, 2023 pm 01:13 PM

The user must enter the order of the two matrices as well as the elements of both matrices. Then, compare the two matrices. Two matrices are equal if both matrix elements and sizes are equal. If the matrices are equal in size but not equal in elements, then the matrices are shown to be comparable but not equal. If the sizes and elements do not match, the display matrices cannot be compared. The following program is a C program, used to compare whether two matrices are equal-#include<stdio.h>#include<conio.h>main(){ intA[10][10],B[10][10]; in

How to reverse the account in the matrix? What does matrix inversion mean? How to reverse the account in the matrix? What does matrix inversion mean? Mar 27, 2024 pm 12:16 PM

In social media operations, matrix account backflow is a common strategy. By directing traffic between different accounts, fans can complement each other and increase their activity. Backflow between matrix accounts requires careful planning and execution, and is not a simple matter. This article will discuss in detail how to implement reversal between different accounts and the significance of matrix inversion. 1. How to reverse the account in the matrix? Among the matrix accounts, it is crucial to choose a main account, which will become the main source of traffic and the platform for publishing core content. Content planning is to formulate corresponding content plans based on account characteristics and target audiences to ensure consistent content quality and style. 3. Recommend and like each other: promote and like each other between matrix accounts, and guide fans through reasonable layout and arrangements.

See all articles