通过将 Matrix 中任意数量的 1 更改为 -1 使所有列的总和为 0 的方法计数
给定一个大小为N x M的矩阵,最初用1填充,任务是通过将任意数量的 1 更改为 -1 来找到使所有列的总和为0的方法的数量。
注意:由于答案可能非常大,因此以 1e9 + 7为模
例子:
Input: N = 2, M = 1
Output: 2
Explanation: Upper example consist 2 rows and 1 column and all cells are filled with 1.
So to make individual column sum = 0, there are 2 possibilities [1, -1] and [-1, 1].
Input: N = 4, M = 2
Output: 36
方法:解决这个问题的想法是基于以下观察:
To make column sum 0 there is need to change N/2 number of 1 to -1. The number of ways to do is X = NCN/2 which can be found using the formula of combinatorics. So total number of ways to make all column sum 0 are XM.
请按照下面给出的插图更好地理解该方法:
插图:
Now consider N = 4 and M = 2
So the matrix is:
1 1
1 1
1 1
1 1
Here each column consist of 4 ones, Consider 1st column to make column sum equal to zero replace two 1 with -1
1. -1 2. -1 3. -1 4. 1 5. 1 6. 1
-1 1 1 -1 -1 1
1 -1 1 -1 1 -1
1 1 -1 1 -1 -1
There are total 4C2 = 6 ways to make the sum of first column sum 0 as seen from above:
For each way to change sum of first column 0 there are 6 ways to make second column sum to 0.
So total number of ways = 6 * 6 = 62 = 36
请按照以下步骤解决问题:
- 使用组合公式N C N/2 = ( N! / ((N/2)! * (N/2)!) )获得单列的可能方式(比如 X)。
- 现在计算X M
- 这将是必需的最终答案。
下面是上述方法的实现:
C++14
// C++ code to implement the approach
#include
using namespace std;
// Function to calculate factorial
long long fact(long long x)
{
long long Fact = 1;
for (long long i = x; i >= 1; i--) {
Fact = (Fact * i);
}
return Fact;
}
// Function to calculate
// total number of ways
long long solve(long long A, long long B)
{
if (A % 2) {
return 0;
}
long long ans = 1, mod = 1000000007;
long long A_C_Aby2
= (fact(A) / (fact(A / 2)
* fact(A / 2)));
for (long long i = 0; i < B; i++) {
ans = (ans * A_C_Aby2) % mod;
}
return ans;
}
// Driver code
int main()
{
long long A = 4;
long long B = 2;
long long ways = solve(A, B);
cout << ways << endl;
}
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to calculate factorial
static long fact(long x)
{
long Fact = 1;
for (long i = x; i >= 1; i--) {
Fact = (Fact * i);
}
return Fact;
}
// Function to calculate
// total number of ways
static long solve( long A, long B)
{
if (A % 2 == 1) {
return 0;
}
long ans = 1, mod = 1000000007;
long A_C_Aby2
= (fact(A) / (fact(A / 2)
* fact(A / 2)));
for (long i = 0; i < B; i++) {
ans = (ans * A_C_Aby2) % mod;
}
return ans;
}
// Driver code
public static void main (String[] args)
{
long A = 4;
long B = 2;
long ways = solve(A, B);
System.out.println(ways);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 code to implement the approach
# Function to calculate factorial
def fact(x):
Fact = 1
for i in range(x, 0, -1):
Fact = (Fact * i)
return Fact
# Function to calculate
# total number of ways
def solve(A, B):
if (A % 2):
return 0
ans, mod = 1, 1000000007
A_C_Aby2 = (fact(A) // (fact(A // 2)
* fact(A // 2)))
for i in range(0, B):
ans = (ans * A_C_Aby2) % mod
return ans
# Driver code
if __name__ == "__main__":
A = 4
B = 2
ways = solve(A, B)
print(ways)
# This code is contributed by rakeshsahni
C#
// C# code to implement the approach
using System;
class GFG {
// Function to calculate factorial
static long fact(long x)
{
long Fact = 1;
for (long i = x; i >= 1; i--) {
Fact = (Fact * i);
}
return Fact;
}
// Function to calculate
// total number of ways
static long solve(long A, long B)
{
if (A % 2 == 1) {
return 0;
}
long ans = 1, mod = 1000000007;
long A_C_Aby2
= (fact(A) / (fact(A / 2) * fact(A / 2)));
for (long i = 0; i < B; i++) {
ans = (ans * A_C_Aby2) % mod;
}
return ans;
}
// Driver code
public static void Main(string[] args)
{
long A = 4;
long B = 2;
long ways = solve(A, B);
Console.WriteLine(ways);
}
}
// This code is contributed by ukasp.
Javascript
36
时间复杂度: O(N + M)
辅助空间: O(1)