Ways of filling matrix such that product of all rows and all columns are equal to unity | PHP program to find number of ways to fill a matrix under given constraints ; Returns a raised power t under modulo mod ; Counting number of ways of filling the matrix ; Function calculating the answer ; if sum of numbers of rows and columns is odd i . e ( n + m ) % 2 == 1 and k = - 1 then there are 0 ways of filiing the matrix . ; If there is one row or one column then there is only one way of filling the matrix ; If the above cases are not followed then we find ways to fill the n - 1 rows and m - 1 columns which is 2 ^ ( ( m - 1 ) * ( n - 1 ) ) . ; Driver Code
< ? php $ mod = 100000007 ; function modPower ( $ a , $ t ) { global $ mod ; $ now = $ a ; $ ret = 1 ; while ( $ t ) { if ( $ t & 1 ) $ ret = $ now * ( $ ret % $ mod ) ; $ now = $ now * ( $ now % $ mod ) ; $ t >>= 1 ; } return $ ret ; } function countWays ( $ n , $ m , $ k ) { global $ mod ; if ( $ k == -1 and ( $ n + $ m ) % 2 == 1 ) return 0 ; if ( $ n == 1 or $ m == 1 ) return 1 ; return ( modPower ( modPower ( 2 , $ n - 1 ) , $ m - 1 ) % $ mod ) ; } $ n = 2 ; $ m = 7 ; $ k = 1 ; echo countWays ( $ n , $ m , $ k ) ; ? >