Codeforces 844B - Rectangles - 思维
2017-08-31 00:40
295 查看
链接:
http://codeforces.com/contest/844/problem/B题目:
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.
Input
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
Output single integer — the number of non-empty sets from the problem description.Examples
input1 1
0
output
1
input
2 3
1 0 1
0 1 0
output
8
Note
In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets.题意:
给你一个n×m的棋盘,上面有0、1两种棋子。同种且同行(或同列)的棋子可以形成一个集合,一个棋子也可以看做一个集合。问你最多可以有多少种集合。思路:
考虑到n和m都比较小,分别枚举每一行的0、1棋子,对于每个棋子有选和不选两种可能。比如说某一行有k个1棋子,那么这一行可以形成的集合的数目就是2k−1,减一是因为要减去一个棋子都不选的这种不合法状态。枚举完行之后列同理。枚举完行和列之后会发现每个集合只有一个一个棋子的情况都被枚举了两遍,所以最终答案再减去n×m即可。
实现:
#include <iostream> #include <algorithm> #include <set> #include <string> #include <vector> #include <queue> #include <map> #include <stack> #include <list> #include <iomanip> #include <functional> #include <sstream> #include <cstdio> #include <cstring> #include <cmath> #include <cctype> #define edl putchar('\n') #define ll long long #define clr(a,b) memset(a,b,sizeof a) #define rep(i,m,n) for(int i=m ; i<=n ; i++) inline int read_() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } namespace FastIO { const int SIZE = 1 << 16; char buf[SIZE], obuf[SIZE], str[60]; int bi = SIZE, bn = SIZE, opt; int read(char *s) { while (bn) {for (; bi < bn && buf[bi] <= ' '; bi++);if (bi < bn) break; bn = fread(buf, 1, SIZE, stdin);bi = 0;}int sn = 0;while (bn) { for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];if (bi < bn) break; bn = fread(buf, 1, SIZE, stdin);bi = 0;}s[sn] = 0;return sn;} bool read(int& x) {int n = read(str), bf;if (!n) return 0; int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1; for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';if (bf < 0) x = -x;return 1;}}; #define read(x) FastIO::read(x) #define read2(x,y) read(x),read(y) #define read3(x,y,z) read(x),read(y),read(z) #define readin read_() using namespace std; const int maxn = 57, mod = int(1e9)+7; int mp[maxn][maxn]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); printf("init! init! init!\n"); #endif int n, m, cnt1, cnt0; read2(n, m); ll ans = 0; rep(i,1,n) rep(j,1,m) read(mp[i][j]); rep(i,1,n) { cnt0 = cnt1 = 0; rep(j,1,m) mp[i][j] ? cnt1++ : cnt0++; ans += (1ll<<cnt0)-1, ans += (1ll<<cnt1)-1; } rep(i,1,m) { cnt0 = cnt1 = 0; rep(j,1,n) mp[j][i] ? cnt1++ : cnt0++; ans += (1ll<<cnt0)-1, ans += (1ll<<cnt1)-1; } printf("%lld\n", ans-n*m); return 0; }
相关文章推荐
- Codeforces 844B - Rectangles(大数据)
- Codeforces 763B-Timofey and rectangles (思维,四色定理)
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- 【CodeForces 689C】Mike and Chocolate Thieves 【二分+ 思维 】
- Codeforces 604 C Alternative Thinking【思维】
- 【Codeforces-402B】-Weird Rounding(数学,思维)
- C - Matrix CodeForces - 365C 思维
- CodeForces - 610A-G - Pasha and Stick-思维
- 周赛二 CodeForces 545B 思维题
- codeforces 370div.2 C Memory and De-Evolution[逆向思维]【思维】
- CodeForces - 670D1【思维】
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- Codeforces 570B Simple Game 概率求解,简单数学思维
- codeforces 954C. Matrix Walk(思维模拟)
- Codeforces 284C Cows and Sequence【思维】
- Codeforces 639C Bear and Polynomials 【思维】
- Maze(CodeForces - 377A )(思维,广搜)
- CodeForces 552C-Vanya and Scales【思维】
- Codeforces 501C:Misha and Forest(好题+思维+异或的运算规律+树)
- codeforces 500c New Year Book Reading 【思维】