You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:
- All cells in a set have the same color.
- Every two cells in a set share row or column.
The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly.
The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.
Output single integer — the number of non-empty sets from the problem description.。
暴力法。
先暴力每一行,对于每一行统计有几个白棋x个。每个棋子可选可不选,所以选择白棋的选法有pow(2,x)-1个。黑棋有pow(2,m-x)-1个选法。
同理计算每一列的。
但是我们行和列都有一种情况,那就是只枚举一个棋子作为集合的选法。所以这是重复的,重复了n*m个。所以减去n*m
#include<cstdio>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int a[100][100];
int n,m;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
long long ans=0;
for(int i=0;i<n;i++)
{
int geshu=0;
for(int j=0;j<m;j++)
{
if(a[i][j])
{
geshu++;
}
}
ans=ans+pow(2,geshu)+pow(2,m-geshu)-2;
}
for(int i=0;i<m;i++)
{
int geshu=0;
for(int j=0;j<n;j++)
{
if(a[j][i])
{
geshu++;
}
}
ans=ans+pow(2,geshu)+pow(2,n-geshu)-2;
}
cout<<ans-n*m<<endl;
}