


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
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)
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; }
Input
3, 15, {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}
Output
36
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!

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

There are significant differences in the learning curves of C# and C and developer experience. 1) The learning curve of C# is relatively flat and is suitable for rapid development and enterprise-level applications. 2) The learning curve of C is steep and is suitable for high-performance and low-level control scenarios.

There are significant differences in how C# and C implement and features in object-oriented programming (OOP). 1) The class definition and syntax of C# are more concise and support advanced features such as LINQ. 2) C provides finer granular control, suitable for system programming and high performance needs. Both have their own advantages, and the choice should be based on the specific application scenario.

Converting from XML to C and performing data operations can be achieved through the following steps: 1) parsing XML files using tinyxml2 library, 2) mapping data into C's data structure, 3) using C standard library such as std::vector for data operations. Through these steps, data converted from XML can be processed and manipulated efficiently.

C# uses automatic garbage collection mechanism, while C uses manual memory management. 1. C#'s garbage collector automatically manages memory to reduce the risk of memory leakage, but may lead to performance degradation. 2.C provides flexible memory control, suitable for applications that require fine management, but should be handled with caution to avoid memory leakage.

C still has important relevance in modern programming. 1) High performance and direct hardware operation capabilities make it the first choice in the fields of game development, embedded systems and high-performance computing. 2) Rich programming paradigms and modern features such as smart pointers and template programming enhance its flexibility and efficiency. Although the learning curve is steep, its powerful capabilities make it still important in today's programming ecosystem.

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers

C# is suitable for projects that require high development efficiency and cross-platform support, while C is suitable for applications that require high performance and underlying control. 1) C# simplifies development, provides garbage collection and rich class libraries, suitable for enterprise-level applications. 2)C allows direct memory operation, suitable for game development and high-performance computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor