【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解

题目链接:https://www.luogu.org/problemnew/show/P1879

状压DP。

设dp[i][j]表示第i行,状态为j的方案数

初始dp[0][0] = 1

这样一共12行12列,最多1<<12。

这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1。

加法原理转移。

$ j&k==0 $
$ dp[i][j] += dp[i-1][k] $

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 5000;
const int mod = 100000000;
ll a[20][maxn], dp[20][maxn], n, m, b[20], ans, is[maxn];
int main()
{
    cin>>n>>m;
    for(ll i = 1; i <= n; i++)
        for(ll j = 1; j <= m; j++) cin>>a[i][j];

    for(ll i = 1; i <= n; i++)
        for(ll j = 1; j <= m; j++)
        if(a[i][j]) b[i] += (1<<(m-j+1));
    for(ll i = 1; i <= n; i++) b[i] >>= 1;

    for(ll i = 0; i < (1 << m); i++)
    if((i&(i<<1)) == 0 && (i&(i>>1)) == 0) is[i] = 1;
    dp[0][0] = 1;
    for(ll i = 1; i <= n; i++)
    {
        for(ll j = 0; j < (1 << m); j++)
        {
            if(is[j] && ((j & b[i]) == j))
            for(ll k = 0; k < (1 << m); k++)
            {
                if((j & k) == 0)
                dp[i][j] = (dp[i][j] + dp[i-1][k])%mod;
            }
        }
    }
    for(ll i = 0; i < (1 << m); i++) ans = (ans + dp[n][i]) % mod;
    cout<<ans%mod;
}

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9920573.html

时间: 2024-08-01 17:26:25

【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解的相关文章

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

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the

P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 这道题跟互不侵犯差不多,但是数据范围更大了 对于状态压缩dp,提前枚举出转移是一种很好的优化 然后对于转移也需要仔仔细细的分析,如同期望dp #include<cstdio> #include<algorithm> #include<iostream> const int mod=1e8; int map[20]; long long f[2][1<<12]; int b[400],tot;

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

P1879 [USACO06NOV]玉米田Corn Fields 状压dp

链接在此! 正解:状压dp(emm……据说插头dp也可以趴但我不管!!!不会!!! 解题报告: ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞下博客没事儿复习下不然做过的题目还花俩小时我真的哭死…… 先放上错误的90分代码讲一下错哪儿了(因为……其实100并不难是可以想到的……没有太大讲的意义,主要我太菜了所以才会搞这么久TT 点我♂看♂沙雕灵巧在线WA题 然后错误的点是最后一个点RE,开

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

题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案) 输出一个整数,即牧

【p1879】[USACO06NOV]玉米田Corn Fields

Description 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用. 遗憾的是,有些土地相当贫瘠,不能用来种草.并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边. John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案) I

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

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

[luoguP1879] [USACO06NOV]玉米田Corn Fields(DP)

传送门 说要统计方案,感觉就是个 Σ 而矩阵中只有 01 ,可以用二进制表示 这样,预处理出每一个每一行所有可能的状态 s 然后初始化第一行所有状态的方案数为 1 f[i][j] = Σf[i - 1][k] (k 和 j 不冲突,j 为第 i 行所有方案,k 为第 i - 1 行所有方案) ——代码 1 #include <cstdio> 2 #include <iostream> 3 #define mod 100000000 4 5 int n, m, ans; 6 int