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

C++Server Side ProgrammingProgramming

<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 &le; x &lt; y &le; 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 &le; x &lt; y &le; n) and swap the rows containing x and y.</p></li><li><p>We have to note that x + y &le; 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 &minus;</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 &minus;</p><pre class="prettyprint notranslate">Define a function dfs(), this will take k, arrays ver and visited, one stack s. &nbsp; &nbsp;if visited[k] is non-zero, then: &nbsp; &nbsp; &nbsp; return &nbsp; &nbsp;visited[k] := true &nbsp; &nbsp;insert k into s &nbsp; &nbsp;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: &nbsp; &nbsp; &nbsp; dfs(*j, ver, visited, s) Define an array f of size: 51. f[0] := 1 for initialize i := 1, when i &lt;= 50, update (increase i by 1), do: &nbsp; &nbsp;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 &lt; n, update (increase i by 1), do: &nbsp; &nbsp;for initialize j := i + 1, when j &lt; n, update (increase j by 1), do: &nbsp; &nbsp; &nbsp; chk := 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for initialize l := 0, when l &lt; n, update (increase l by 1), do: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mat[i, l] + mat[j, l]) &gt; k, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;chk := 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Come out from the loop &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if chk is same as 0, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;insert j at the end of pk[i] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;insert i at the end of pk[j] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chk := 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for initialize l := 0, when l &lt; n, update (increase l by 1), do: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (mat[l, i] + mat[l, j]) &gt; k, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chk := 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Come out from the loop &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if chk is same as 0, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;insert j at the end of e[i] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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 &lt; n, update (increase i by 1), do: &nbsp; &nbsp;v1[i] := false &nbsp; &nbsp;v2[i] := false for initialize i := 0, when i &lt; n, update (increase i by 1), do: &nbsp; &nbsp;Define one stack s. &nbsp; &nbsp;if not v1[i] is non-zero, then: &nbsp; &nbsp; &nbsp; dfs(i, pk, v1, s) &nbsp; &nbsp; &nbsp; if not s is empty, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resa := resa * (f[size of s]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resa := resa mod modval for initialize i := 0, when i &lt; n, update (increase i by 1), do: &nbsp; &nbsp;Define one stack s &nbsp; &nbsp;if not v2[i] is non-zero, then: &nbsp; &nbsp; &nbsp; dfs(i, e, v2, s) &nbsp; &nbsp; &nbsp; if not s is empty, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resb := resb * (f[size of s]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resb := resb mod modval print((resa * resb) mod modval)</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;bits/stdc++.h&gt; using namespace std; #define modval 998244353 const int INF = 1e9; void dfs(int k, vector&lt;int&gt; ver[], bool visited[], stack&lt;int&gt; &amp;s) { &nbsp; &nbsp;if(visited[k]) &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp;visited[k] = true; &nbsp; &nbsp;s.push(k); &nbsp; &nbsp;for(vector&lt;int&gt; :: iterator j = ver[k].begin(); j!=ver[k].end(); j++) &nbsp; &nbsp; &nbsp; dfs(*j, ver, visited, s); } void solve(int n, int k, vector&lt;vector&lt;int&gt;&gt; mat) { &nbsp; &nbsp;int f[51]; &nbsp; &nbsp;f[0] = 1; &nbsp; &nbsp;for(int i = 1; i &lt;= 50; i++) { &nbsp; &nbsp; &nbsp; f[i] = (i * f[i-1]) % modval; } vector&lt;int&gt; e[n]; vector&lt;int&gt; pk[n]; for(int i = 0; i &lt; n; i++) { &nbsp; &nbsp; for(int j = i + 1;j &lt; n; j++) { &nbsp; &nbsp; &nbsp; int chk = 0; &nbsp; &nbsp; &nbsp; for(int l = 0; l &lt; n; l++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if((mat[i][l] + mat[j][l]) &gt; k) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chk = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if(chk==0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pk[i].push_back(j); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pk[j].push_back(i); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; chk = 0; &nbsp; &nbsp; &nbsp; for(int l = 0;l &lt; n; l++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if((mat[l][i] + mat[l][j]) &gt; k){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chk = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if(chk == 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e[i].push_back(j); &nbsp; &nbsp; &nbsp; &nbsp; e[j].push_back(i); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } } int resa = 1, resb = 1; bool v1[n], v2[n]; for(int i = 0; i &lt; n; i++) { &nbsp; &nbsp;v1[i] = false; &nbsp; &nbsp; v2[i] = false; } for(int i = 0;i &lt; n; i++) { &nbsp; &nbsp;stack&lt;int&gt; s; &nbsp; &nbsp;if(!v1[i]) { &nbsp; &nbsp; &nbsp; dfs(i, pk, v1, s); &nbsp; &nbsp; &nbsp; if(!s.empty()) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resa *= (f[s.size()]) % modval; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resa %= modval; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } } for(int i = 0 ;i &lt; n; i++) { &nbsp; &nbsp;stack&lt;int&gt; s; &nbsp; &nbsp;if(!v2[i]){ &nbsp; &nbsp; &nbsp; dfs(i, e, v2, s); &nbsp; &nbsp; &nbsp; if(!s.empty()) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resb *= (f[s.size()]) % modval; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resb %= modval; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} } cout&lt;&lt; (resa * resb) % modval; } int main() { &nbsp; &nbsp;int n = 3, k = 15; &nbsp; &nbsp;vector&lt;vector&lt;int&gt;&gt; mat = {{4, 3, 6}, {5, 9, 7}, {1, 2, 8}}; &nbsp; &nbsp;solve(n, k, mat); &nbsp; &nbsp;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>
raja
Updated on 25-Feb-2022 10:31:41

Advertisements