Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int n, m;
- bool a[25][15];
- bool ok[25][1 << 10];
- bool used[25];
- bool can[25][1 << 10];
- int st[25];
- ll dp[25][1 << 10];
- int main() {
- ios_base::sync_with_stdio(false);
- //freopen("input.txt", "r", stdin);
- cin >> n >> m;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> a[i][j];
- }
- }
- for (int row = 0; row < n; row++) {
- for (int j = 0; j < m; j++) {
- if (a[row][j] == 1) st[row] |= (1 << j);
- }
- for (int mask = 0; mask < (1 << m); mask++) {
- int mask2 = (mask & st[row]);
- if (mask2 != st[row]) continue;
- can[row][mask] = true;
- ok[row][mask] = true;
- memset(used, 0, sizeof used);
- for (int bit = 0; bit < m; bit++) {
- int val1 = (mask >> bit) & 1;
- int val2 = (mask >> (bit + 1)) & 1;
- if (used[bit] || a[row][bit] || (val1 == 1)) continue;
- used[bit] = true;
- if ((bit == m - 1) || (val2 == 1)) {
- ok[row][mask] = false;
- }
- used[bit + 1] = true;
- }
- }
- }
- dp[0][st[0]] = 1;
- if (n == 1) {
- if (ok[0][st[0]]) cout << 1;
- else cout << 0;
- return 0;
- }
- for (int row = 0; row < n - 1; row++) {
- for (int mask1 = 0; mask1 < (1 << m); mask1++) {
- if (!can[row][mask1]) continue;
- for (int mask2 = 0; mask2 < (1 << m); mask2++) {
- if ((mask1 & mask2) != 0) continue;
- if ((mask2 & st[row + 1]) != 0) continue;
- int nmask = mask1 | mask2;
- if (!ok[row][nmask]) continue;
- dp[row + 1][mask2 | st[row + 1]] += dp[row][mask1];
- }
- }
- }
- ll ans = 0;
- for (int mask = 0; mask < (1 << m); mask++) {
- if (ok[n - 1][mask]) ans += dp[n - 1][mask];
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement