[USACO 06NOV]Corn Fields

Posted NaVi_Awson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO 06NOV]Corn Fields相关的知识,希望对你有一定的参考价值。

Description

题库链接

给你一个 \(0,1\) 矩阵,只准你在 \(1\) 上放物品;并且要满足物品不能相邻。允许空放,问方案数,取模。

\(1\leq n,m\leq 12\)

Solution

状压 \(DP\)

\(f_{i,t}\) 为处理到第 \(i\) 行时放物品的状态为 \(t\) 的方案数。

若第 \(i\) 行的 \(1\) 的状态为 \(a\) 。显然由于必须放在 \(1\) 上,所以 \(a~and~t = a\) ,其中 \(and\) 为按位与;并且由于同行之间不能相邻,故 \((t>>1)~and~t=0\)

枚举的上一行状态为 \(k\) 。显然 \(t~and~k=0\)

复杂度为 \(O(n\cdot (2^n)^2)\)

Code

//It is made by Awson on 2018.2.25
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int yzh = 100000000;
const int size = 1<<12;
void read(int &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }

int n, m, a[15], x, f[15][size+5], bin[15];

void work() {
    read(n), read(m); bin[0] = 1; for (int i = 1; i <= 12; i++) bin[i] = bin[i-1]<<1;
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) read(x), a[i] = ((a[i]<<1)|x);
    f[0][0] = 1;
    for (int i = 1; i <= n; i++) {
    for (int j = 0; j < bin[m]; j++)
        if ((j == 0 || ((j|a[i]) == a[i])) && (j&(j>>1)) == 0)
        for (int k = 0; k < bin[m]; k++)
            if ((k == 0 || ((k|a[i-1]) == a[i-1])) && (k&(k>>1)) == 0)
            if ((j&k) == 0) f[i][j] = (f[i][j]+f[i-1][k])%yzh;
    }
    int ans = 0;
    for (int i = 0; i < bin[m]; i++) ans = (ans+f[n][i])%yzh;
    writeln(ans);
}
int main() {
    work(); return 0;
}

以上是关于[USACO 06NOV]Corn Fields的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

[USACO06NOV]Corn Fields(状压DP)

状压DP水题[USACO06NOV]玉米田Corn Fields

[USACO 06NOV]Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields题解(注释版)