Ways of filling matrix such that product of all rows and all columns are equal to unity
Last Updated :
25 Jul, 2022
We are given three values n , m and k where n is number of rows in matrix, m is number of columns in the matrix and k is the number that can have only two values -1 and 1. Our aim is to find the number of ways of filling the matrix of n \times m such that the product of all the elements in each row and each column is equal to k . Since the number of ways can be large we will output ans \mod{1000000007}
Examples:
Input : n = 2, m = 4, k = -1
Output : 8
Following configurations satisfy the conditions:-


Input : n = 2, m = 1, k = -1
Output : The number of filling the matrix
are 0
From the above conditions, it is clear that the only elements that can be entered in the matrix are 1 and -1. Now we can easily deduce some of the corner cases
- If k = -1, then the sum of number of rows and columns cannot be odd because -1 will be present odd
number of times in each row and column therefore if the sum is odd then answer is 0 . - If n = 1 or m = 1 then there is only one way of filling the matrix therefore answer is 1.
- If none of the above cases are applicable then we fill the first n-1 rows and the first m-1 columns with 1 and -1. Then the remaining numbers can be uniquely identified since the product of each row an each column is already known therefore the answer is 2 ^ {(n-1) \times (m-1)} .
Implementation:
C++
// CPP program to find number of ways to fill
// a matrix under given constraints
#include <bits/stdc++.h>
using namespace std;
#define mod 100000007
/* Returns a raised power t under modulo mod */
long long modPower(long long a, long long t)
{
long long now = a, ret = 1;
// Counting number of ways of filling the matrix
while (t) {
if (t & 1)
ret = now * (ret % mod);
now = now * (now % mod);
t >>= 1;
}
return ret;
}
// Function calculating the answer
long countWays(int n, int m, int k)
{
// 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 (k == -1 && (n + m) % 2 == 1)
return 0;
// If there is one row or one column then there
// is only one way of filling the matrix
if (n == 1 || m == 1)
return 1;
// 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)).
return (modPower(modPower((long long)2, n - 1),
m - 1) % mod);
}
// Driver function for the program
int main()
{
int n = 2, m = 7, k = 1;
cout << countWays(n, m, k);
return 0;
}
Java
// Java program to find number of ways to fill
// a matrix under given constraints
import java.io.*;
class Example {
final static long mod = 100000007;
/* Returns a raised power t under modulo mod */
static long modPower(long a, long t, long mod)
{
long now = a, ret = 1;
// Counting number of ways of filling the
// matrix
while (t > 0) {
if (t % 2 == 1)
ret = now * (ret % mod);
now = now * (now % mod);
t >>= 1;
}
return ret;
}
// Function calculating the answer
static long countWays(int n, int m, int k)
{
// 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 (n == 1 || m == 1)
return 1;
// If there is one row or one column then
// there is only one way of filling the matrix
else if ((n + m) % 2 == 1 && k == -1)
return 0;
// 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)).
return (modPower(modPower((long)2, n - 1, mod),
m - 1, mod) % mod);
}
// Driver function for the program
public static void main(String args[]) throws IOException
{
int n = 2, m = 7, k = 1;
System.out.println(countWays(n, m, k));
}
}
Python3
# Python program to find number of ways to
# fill a matrix under given constraints
# Returns a raised power t under modulo mod
def modPower(a, t):
now = a;
ret = 1;
mod = 100000007;
# Counting number of ways of filling
# the matrix
while (t):
if (t & 1):
ret = now * (ret % mod);
now = now * (now % mod);
t >>= 1;
return ret;
# Function calculating the answer
def countWays(n, m, k):
mod= 100000007;
# 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 (k == -1 and ((n + m) % 2 == 1)):
return 0;
# If there is one row or one column then
# there is only one way of filling the matrix
if (n == 1 or m == 1):
return 1;
# 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)).
return (modPower(modPower(2, n - 1),
m - 1) % mod);
# Driver Code
n = 2;
m = 7;
k = 1;
print(countWays(n, m, k));
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program to find number of ways to fill
// a matrix under given constraints
using System;
class Example
{
static long mod = 100000007;
// Returns a raised power t
// under modulo mod
static long modPower(long a, long t,
long mod)
{
long now = a, ret = 1;
// Counting number of ways
// of filling the
// matrix
while (t > 0)
{
if (t % 2 == 1)
ret = now * (ret % mod);
now = now * (now % mod);
t >>= 1;
}
return ret;
}
// Function calculating the answer
static long countWays(int n, int m,
int k)
{
// 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 (n == 1 || m == 1)
return 1;
// If there is one row or one
// column then there is only
// one way of filling the matrix
else if ((n + m) % 2 == 1 && k == -1)
return 0;
// 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)).
return (modPower(modPower((long)2, n - 1,
mod), m - 1, mod) % mod);
}
// Driver Code
public static void Main()
{
int n = 2, m = 7, k = 1;
Console.WriteLine(countWays(n, m, k));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find number
// of ways to fill a matrix under
// given constraints
$mod = 100000007;
// Returns a raised power t
// under modulo mod
function modPower($a, $t)
{
global $mod;
$now = $a; $ret = 1;
// Counting number of ways
// of filling the matrix
while ($t)
{
if ($t & 1)
$ret = $now * ($ret % $mod);
$now = $now * ($now % $mod);
$t >>= 1;
}
return $ret;
}
// Function calculating the answer
function countWays($n, $m, $k)
{
global $mod;
// 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 ($k == -1 and ($n + $m) % 2 == 1)
return 0;
// If there is one row or
// one column then there
// is only one way of
// filling the matrix
if ($n == 1 or $m == 1)
return 1;
// 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)).
return (modPower(modPower(2, $n - 1),
$m - 1) % $mod);
}
// Driver Code
$n = 2;
$m = 7;
$k = 1;
echo countWays($n, $m, $k);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to find number of
// ways to fill a matrix under given
// constraints
let mod = 100000007;
// Returns a raised power t under modulo mod
function modPower(a, t, mod)
{
let now = a, ret = 1;
// Counting number of ways of
// filling the matrix
while (t > 0)
{
if (t % 2 == 1)
ret = now * (ret % mod);
now = now * (now % mod);
t >>= 1;
}
return ret;
}
// Function calculating the answer
function countWays(n, m, k)
{
// 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 (n == 1 || m == 1)
return 1;
// If there is one row or one column then
// there is only one way of filling the matrix
else if ((n + m) % 2 == 1 && k == -1)
return 0;
// 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)).
return (modPower(modPower(2, n - 1, mod),
m - 1, mod) % mod);
}
// Driver Code
let n = 2, m = 7, k = 1;
document.write(countWays(n, m, k));
// This code is contributed by code_hunt
</script>
Output:
64
Time complexity: .
Space Complexity : O(1).
Similar Reads
Count of ways to generate a Matrix with product of each row and column as 1 or -1 Given two integers N and M, the task is to find the numbers of ways to form a matrix of size N * M consisting only of 1 or -1, such that the product of integers in each row and each column is equal to 1 or -1. Examples: Input: N = 2, M = 2 Output: 4 Explanation: Possible ways to get product of each
5 min read
Number of ways of cutting a Matrix such that atleast one cell is filled in each part Given an integer K and a matrix mat[][] containing 1 and 0, where 1 denotes the cell is filled and 0 denotes an empty cell. The task is to find the number of ways to cut the matrix into K parts using K-1 cuts such that each part of the matrix contains atleast one filled cell. For each cut, there mus
12 min read
Enlarge a Matrix such that each element occurs in R rows and C columns Given a matrix arr[][] of size N x M, and two numbers R and C, the task is to enlarge this matrix such that each element of the original matrix occurs in R rows and C columns in the enlarged matrix. Examples: Input: arr[][] = {{1, 2, 3}, {4, 5, 6}} R = 3, C = 2 Output: 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2
9 min read
Product of middle row and column in an odd square matrix Given an integer square matrix of odd dimensions (3 * 3, 5 * 5). The task is to find the product of the middle row & middle column elements. Examples: Input: mat[][] = {{2, 1, 7}, {3, 7, 2}, {5, 4, 9}} Output: Product of middle row = 42 Product of middle column = 28 Explanation: Product of Middl
6 min read
Construct a square matrix such that the sum of each row and column is odd Given an integer N. Then your task is to output a square matrix of length N using the numbers from the range [1, N2] such that the sum of each row and column is odd. Note: If there are multiple solution, then print any of them. Examples: Input: N = 2 Output: {{1, 2}, {4, 3}} Explanation: Let us calc
11 min read
Construct a Binary Matrix whose sum of each row and column is a Prime Number Given an integer N, the task is to construct a binary matrix of size N * N such that the sum of each row and each column of the matrix is a prime number. Examples: Input: N = 2 Output: 1 1 1 1 Explanation: Sum of 0th row = 1 + 1 = 2 (Prime number) Sum of 1st row = 1 + 1 = 2 (Prime number) Sum of 0th
6 min read