

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to find out the number of unique matrices that can be generated by swapping rows and columns
<p>Suppose, we have a n x n matrix. Each element in the matrix is unique and is an integer number between 1 and n<sup>2</sup>. Now we can perform the operations below in any amount and any order.</p><ul class="list"><li><p>We pick any two integers x and y that are in the matrix, where (1 ≤ x < y ≤ n) and swap the columns containing x and y.</p></li><li><p>We pick any two integers x and y that are in the matrix, where (1 ≤ x < y ≤ n) and swap the rows containing x and y.</p></li><li><p>We have to note that x + y ≤ k and the values must not be present in the same rows and columns.</p></li></ul><p>We have to find out the number of unique matrices that can be obtained by performing the operations.</p><p>So, if the input is like n = 3, k = 15, mat = {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}, then the output will be 36.</p><p>For example, the two values picked are x = 3 and y = 5. The resultant matrix if the columns are swapped will be −</p><pre class="result notranslate">3 4 6 9 5 7 2 1 8</pre><p>36 such unique matrices can be obtained this way.</p><p>To solve this, we will follow these steps −</p><pre class="prettyprint notranslate">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)</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding −</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#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; }</pre><h2>Input</h2><pre class="result notranslate">3, 15, {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}</pre><h2>Output</h2><pre class="result notranslate">36</pre>
- Related Questions & Answers
- C++ program to find out the number of coordinate pairs that can be made
- C++ program to find out the maximum number of cells that can be illuminated
- Program to find out number of blocks that can be covered in Python
- Count unique numbers that can be generated from N by adding one and removing trailing zeros in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Program to find maximum number of package that can be bought by buyers in C++
- Program to find number of possible BSTs can be generated using n distinct nodes in Python
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- Python Program to find out the number of rooms in which a prize can be hidden
- C++ program to find out the number of ways a grid with boards can be colored
- Max Number By Swapping
- C++ code to find out which number can be greater
- Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
Advertisements