BZOJ 1087 题解【状压DP】
1087: [SCOI2005]互不侵犯King
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 3112 Solved: 1816
[Submit][Status][Discuss]
Description
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上
左下右上右下八个方向上附近的各一个格子,共8个格子。
Input
只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)
Output
方案数。
Sample Input
Sample Output
HINT
Solution
这是一道状压DP题,本蒟蒻第一次做状压,坑了好久。
我们用0和1代表棋盘上的某点是否放棋子,每一行的状态都可以用唯一的一个十进制数表示,我们可以通过位运算,得到合法状态数,并统计即可。
DP方程:
f[ i + 1 ][ p + num[ y ] ][ y ] += f[ i ][ p ][ x ]
/**************************************************************
Problem: 1087
User: shadowland
Language: C++
Result: Accepted
Time:40 ms
Memory:6336 kb
****************************************************************/ #include "bits/stdc++.h" using namespace std ;
const int maxNum = ;
const int maxN = ; bool feasible[ maxNum << ] , feasible_flat[ maxNum << ][ maxNum << ] ;
long long f[ maxN ][ ][ maxNum ] , num[ maxNum << ] ; long long Ans ; inline bool Check ( const int x , const int y ) {
if ( ( ( x & y ) == ) && ( ( x & ( y >> ) ) == ) && ( ( x & ( y << ) ) == ) ) return true ;
else return false ;
} void Init ( const int N , const int M ) {
int _cnt = ;
for ( int i= ; i<=( << N ) - ; ++i ) {
if ( ( i & ( i << ) ) == ) {//状态合法记录
_cnt = ;
for ( int Base = i ; Base ; Base >>= ) _cnt += ( Base & ) ;
num[ i ] = _cnt ;//
feasible[ i ] = true ;
}
}
for ( int i= ; i<=( << N ) - ; ++i ) {
if ( feasible[ i ] ) {
for ( int j= ; j<=( << N ) - ; ++j ) {
if ( feasible[ j ] ) {
if ( Check ( i , j ) ) {
feasible_flat[ i ][ j ] = true ;
}
}
}
}
}
} void DEBUG_( int n , int m ) {
printf ( "\n" ) ;
for ( int i= ; i<=( << n ) - ; ++i )
printf ( "%d " , feasible[ i ] ) ; printf ( "\n" ) ;
for ( int i= ; i<=( << n ) - ; ++i ) {
for ( int j= ; j<=( << n ) - ; ++j ) {
printf ( "%d " , feasible_flat[ i ][ j ] ) ;
}
printf ( "\n" ) ;
}
} void DEBUG___( int n , int m ) {
for ( int i= ; i<=n ; ++i ) {
for ( int j= ; j<=( << n ) - ; ++j ) {
for ( int k= ; k<= ( << n ) - ; ++k ) {
printf ( "%d ",f[i][j][k]);
}
}
}
}
int main ( ) {
int N , M ;
scanf ( "%d %d" , &N , &M ) ;
Init( N , M ) ;
for ( int i= ; i<=( << N ) - ; ++i ) {//第一行的所有合法状态
if ( feasible[ i ] ) {
f[ ][ num[ i ] ][ i ] = ;
}
}
for ( int j = ; j<N ; ++j ) {
for ( int x = ; x<= ( << N ) - ; ++x ) {
if ( feasible[ x ] ) {//x状态合法
for ( int y= ; y<= ( << N ) - ; ++y ) {
if ( feasible[ y ] ) {//y状态合法
if ( feasible_flat[ x ][ y ] ) {
for ( int p=num[ x ] ; p + num[ y ] <=M ; ++p ) {
f[ j + ][ p + num[ y ] ][ y ] += f[ j ][ p ][ x ] ;
}
}
}
}
}
}
}
//DEBUG_( N , M ) ;
//DEBUG___( N , M ) ;
for ( int i= ; i<= ( << N ) - ; ++i ) {
Ans += f[ N ][ M ][ i ] ;//统计最终合法状态
}
cout << Ans << endl ;
return ;
}
一定要开long long 。不开long long 见祖宗,十年OI一场空。
2016-10-12 23:20:05
(完)
BZOJ 1087 题解【状压DP】的更多相关文章
- BZOJ 2064: 分裂( 状压dp )
n1+n2次一定可以满足..然后假如之前土地集合S1的子集subs1和之后土地集合S2的子集subs2相等的话...那么就少了2个+操作...所以最后答案就是n1+n2-少掉的最多操作数, 由状压dp ...
- O - Matching 题解(状压dp)
题目链接 题目大意 给你一个方形矩阵mp,边长为n(n<=21) 有n个男生和女生,如果\(mp[i][j]=1\) 代表第i个男生可以和第j个女生配对 问有多少种两两配对的方式,使得所有男生和 ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- BZOJ 3812 主旋律 (状压DP+容斥) + NOIP模拟赛 巨神兵(obelisk)(状压DP)
这道题跟另一道题很像,先看看那道题吧 巨神兵(obelisk) 题面 欧贝利斯克的巨神兵很喜欢有向图,有一天他找到了一张nnn个点mmm条边的有向图.欧贝利斯克认为一个没有环的有向图是优美的,请问这张 ...
- NOIP2017 宝藏 题解报告【状压dp】
题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏.但是 ...
- TZOJ 2289 Help Bob(状压DP)
描述 Bob loves Pizza but is always out of money. One day he reads in the newspapers that his favorite ...
- 【bzoj5161】最长上升子序列 状压dp+打表
题目描述 现在有一个长度为n的随机排列,求它的最长上升子序列长度的期望. 为了避免精度误差,你只需要输出答案模998244353的余数. 输入 输入只包含一个正整数n.N<=28 输出 输出只包 ...
- TZOJ 4912 炮兵阵地(状压dp)
描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P" ...
- POJ 1684 Corn Fields(状压dp)
描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...
- 【uoj#37/bzoj3812】[清华集训2014]主旋律 状压dp+容斥原理
题目描述 求一张有向图的强连通生成子图的数目对 $10^9+7$ 取模的结果. 题解 状压dp+容斥原理 设 $f[i]$ 表示点集 $i$ 强连通生成子图的数目,容易想到使用总方案数 $2^{sum ...
随机推荐
- 22.访问者模式(Vistor Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { /// <summary> /// 访问者模 ...
- Android 5.0 如何正确启用isLoggable(一)__使用详解
转自:http://blog.csdn.net/yihongyuelan/article/details/46409389 isLoggable是什么 在Android源码中,我们经常可以看到如下代码 ...
- MVC4 遇到问题总结
1.路径编写: 举例1.<img src="../Login/VailCode" width="108" height="40"&g ...
- 【翻译十五】-java并发之固定对象与实例
Immutable Objects An object is considered immutable if its state cannot change after it is construct ...
- C#DataGridView合计处理
网上查了一些关于合计的代码 ,但发现大都都不尽人意,就自己再根据资料改了一下. #region 合计 //调用方法示例 //HeJi heji = null; //heji = new HeJi(la ...
- x264中I,P,B帧和PTS,DTS的关系
转自:http://www.cppblog.com/tx7do/archive/2013/01/30/197633.html 基本概念: I frame :帧内编码帧 又称intra picture, ...
- Mysql数据库操作系统及配置参数优化
数据库结构优化 表的水平拆分常用的水平拆分方法为:1.对 customer_id进行 hash运算,如果要拆分成5个表 则使用mod(customer_id,5)取出0-4个值2.针对不同的 hash ...
- Asp.Net MVC中Controller与View之间传递的Model
Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- aChartEngine图表显示(一页显示多张图表)
在看本篇的时候,请确认已经看过了 某android平板项目开发笔记----aChartEngine图表显示(1) 不然,有些地方这里就不再说明… 关于XYMutilpleSeriesDataset 一 ...