码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO 06NOV]Corn Fields

时间:2018-02-25 19:21:24      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:utc   put   lex   read   desc   double   char   owb   http   

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

标签:utc   put   lex   read   desc   double   char   owb   http   

原文地址:https://www.cnblogs.com/NaVi-Awson/p/8469712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!