【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;
}
【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解的更多相关文章
- 洛谷 P1879 [USACO06NOV]玉米田Corn Fields 题解
P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...
- luogu P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- C++ 洛谷 P1879 [USACO06NOV]玉米田Corn Fields
没学状压DP的看一下 合法布阵问题 P1879 [USACO06NOV]玉米田Corn Fields 题意:给出一个n行m列的草地(n,m<=12),1表示肥沃,0表示贫瘠,现在要把一些牛放在 ...
- P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...
- 洛谷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 ...
- P1879 [USACO06NOV]玉米田Corn Fields (状压dp入门)
题目链接: https://www.luogu.org/problemnew/show/P1879 具体思路: 我们可以先把所有合法的情况枚举出来,然后对第一行判断有多少种情况满足,然后对于剩下的行数 ...
- 洛谷 P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
随机推荐
- Oracle查看表空间容量
select /*+ no_merge */ al.tablespace_name, round(al.currsizemb) currsizemb, round(al.maxsizemb) maxs ...
- js 防止连续点击
简称 js防连点 var flag = true; $(".yzm>span").click(function(){ if(!flag){ return fals ...
- BZOJ1258 [CQOI2007]三角形
Description 画一个等边三角形,把三边的中点连接起来,得到四个三角形,把它们称为T1,T2,T3,T4,如图1.把前三个三角形也这样划分,得到12个更小的三角形:T11,T12,T13,T1 ...
- BZOJ4598: [Sdoi2016]模式字符串(点分治 hash)
题意 题目链接 Sol 直接考虑点分治+hash匹配 设\(up[i]\)表示\(dep \% M = i\)的从下往上恰好与前\(i\)位匹配的个数 \(down\)表示\(dep \% M = i ...
- jQuery轮播图(二)利用构造函数和原型创建对象以实现继承
本文是在我开始学习JavaScript继承时,对原型继承的一些理解和运用.文中所述的继承方式均是使用js特有的原型链方式,实际上有了ES6的类之后,实现继承的就变得十分简单了,所以这种写法现在也不在推 ...
- curl 模拟发起百度地图API post请求
注:开始做的是get请求,比较简单,然后又查询了一番就做成了post请求,有几个地方特别说明一下: 一,$address,是必须传的,$city可不传: 二,ak跟之前的key一直,需要申请,我的好像 ...
- 使用postMessage通信,未触发message事件
前提: 父子页面跨域通信,使用postMessage技术 a页面为父页面,b页面为子页面 a中包含多个iframe,部分域名是相同的,目录层级不一致,地址使用变量根据触发的条件不同拼接地址 部分代码( ...
- FormData js对象的介绍和使用
FormData js对象的介绍和使用 FormData对象,可以把所有表单元素的name与value组成一个queryString,提交到后台. 在使用ajax提交时,使用FormData对象可以减 ...
- LeetCode 514----Freedom Trail
问题描述 In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a ...
- Oracle客户端安装以及PL/SQL Developer安装方法
1,安装Oracle客户端 2,配置数据库,如下: 安装路径:D:\app\ThinkPad\product\11.2.0\client_1\network\admin 建立文件:tnsnames.o ...