【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐搭积木(状压dp)

题干:
 

小乐乐想要给自己搭建一个积木城堡。

积木城堡我们假设为n*m的平面矩形。

小乐乐现在手里有1*2,2*1两种地砖。

小乐乐想知道自己有多少种组合方案。

 

输入描述:

第一行输入整数n,m。(1<=n,m<=10)

输出描述:

输出组合方案数。

示例1

输入

复制

2 3

输出

复制

3

说明

 

示例2

输入

复制

1 3

输出

复制

0

示例3

输入

复制

2 5

输出

复制

8

解题报告:

   这题状压dp,,那种很传统的方法就不贴了,,今天来贴一个更快的方法、、不同点不在于预处理第一行,而是下面的行,也就是直接找到第二行所有符合的状态,顺便找到对应上一行应该有的状态,直接做和就可以了。

AC代码:

/*优化:不去盲目的列举所有状态i和j然后判断状态j能否到达i,这样效率很低,因为能到达i的状态j很少
因此对于每种状态i,由i区搜索能到达i的状态j,大大提高效率
有298ms->32ms
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 0x3f3f3f3f
typedef long long LL;
using namespace std;const int MAX=(1<<11)+10;
int n,m;
LL temp[MAX],dp[MAX],biao[15];
bool check(int i){while(i){if(i&1){i>>=1;if(!(i&1))return false;//第j列是1则第j+1列必须是1i>>=1;//继续判断下一列}else i>>=1;//继续判断下一列}return true;
}
void Init(){memset(temp,0,sizeof temp);for(int i=0;i<biao[m];++i)if(check(i))temp[i]=1;//初始化第一行
}
void dfs(int lie,int now,int cur) {if(lie == m) {dp[now] += temp[cur];return ;}if((now>>lie) & 1) {dfs(lie+1,now,cur);if((now>>(lie+1)) & 1) dfs(lie+2,now,cur|(1<<lie)|(1<<(lie+1)));}else dfs(lie+1,now,cur|(1<<lie));
}
void DP(){for(int k=2;k<=n;++k){for(int i=0;i<biao[m];++i) dp[i]=0;for(int i=0;i<biao[m];++i) dfs(0,i,0);for(int i=0;i<biao[m];++i) temp[i]=dp[i];}
}int main(){biao[0]=1;for(int i=1;i<12;++i)biao[i]=2*biao[i-1];scanf("%d%d",&n,&m);//if(n<m)swap(n,m);//始终保持m<n,提高效率Init();DP();printf("%lld\n",temp[biao[m]-1]);//输出最后一行到达时的状态必须全部是1return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441082.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 packagestar20110526;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io…

【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干&#xff1a; FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 < p < n and 0 < q < n. At each grid location Fatmouse has hid between 0 and 10…

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java求小数高精度_浅谈Java中的高精度整数和高精度小数

在实际编码中&#xff0c;会遇到很多高精度的事例&#xff0c;比如&#xff0c;在计算金钱的时候就需要保留高精度小数&#xff0c;这样计算才不会有太大误差&#xff1a;在下面的代码中&#xff0c;我们验证了&#xff0c;当两个float型的数字相加&#xff0c;得到的结果和我们…

【CodeForces - 298B 】Sail (模拟,题意)

题干&#xff1a; The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)

题干&#xff1a; Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners)…

php网站的编辑器,5款适合PHP使用的HTML编辑器推荐

概述Web开发中&#xff0c;很多地方都会用到HTML编辑器(富文本编辑器)&#xff0c;我也用过几种&#xff0c;如UEditor、CkEditor等。这几天看了几篇文章&#xff0c;都是关于HTML编辑器的&#xff0c;写个文章记录下。推荐的编辑器simditor这个编辑器是前几天刚看到的&#xf…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)

题干&#xff1a; 小乐乐特别喜欢25这个数字&#xff0c;他想把所有的数字都变成25的倍数。 现在小乐乐得到一个数字&#xff0c;想问问你最少用几次操作才可以把这个数字改造成25的倍数。 对于一次操作我们可以把相邻的两位做交换&#xff0c;比如123经过一次操作之后就可以…

怎么表示一个PHP语句块,php switch语句多个值匹配同一代码块应用示例

先说说switch()语句的格式switch(表达式){case 匹配1&#xff1a;当匹配1和表达式匹配成功执行的代码;break;case 匹配2&#xff1a;当匹配2和表达式匹配成功执行的代码;break;default&#xff1a;如果case语句没有与表达式成功所执行的代码;}理解 switch 是怎样执行的非常重要…

php查找顶级分类,php 无限级分类 获取顶级分类ID,php顶级_PHP教程

php 无限级分类 获取顶级分类ID&#xff0c;php顶级有这样一个表&#xff0c;id是分类的ID&#xff0c;name是分类名称&#xff0c;pid是上级分类的ID。现在有个分类ID&#xff0c;程序要找到它上级的上级的上级……分类的ID&#xff0c;简单说就是找出顶级分类的ID。比如“新鲜…

【POJ - 2392】Space Elevator (dp,优秀的背包问题)

题干&#xff1a; The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 < K < 400) different types of blocks with which to build the tower. Each block of type i has heigh…

php三种web开发技术,三种WEB开发主流技术ASP-PHP-JSP的评价

三种Web开发主流技术ASP-PHP-JSP的评价[摘要]本文从程序开发者的角度&#xff0c;对现今社会盛行的WEB开发技术进行了分析&#xff0c;旨在让WEB 开发人员、使用者、准备学习的人对WEB开发技术有更加深入的了解&#xff0c;并为WEB应用程序开发提供指导。[关键词]WEB开发技术 A…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享

搜索热词一、函数实现实例1&#xff1a;代码如下:function time_tran($the_time){$now_time date("Y-m-d H:i:s",time()8*60*60);$now_time strtotime($now_time);$show_time strtotime($the_time);$dur $now_time - $show_time;if($dur < 0){return $the_tim…

【CodeForces - 608D】Zuma(区间dp)

题干&#xff1a; Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Geno…

matlab没有int函数,matlab 未定义与 'char' 类型的输入参数相对应的函数 'int'。

最后你那zd句plot(int(k),double(s));里的int(k);不对。你如果想以k为横坐标&#xff0c;直接把int去掉&#xff0c;如回果非想要变成整数可以用floor(k);注&#xff1a;fix&#xff1a;向零取整 floor&#xff1a;向小取整 round&#xff1a;四舍五入 ceil&#xff1a;向...如…

【CodeForces - 347C 】Alice and Bob (思维,数学,等差数列)

题干&#xff1a; It is so boring in the summer holiday, isnt it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each …

java读取yaml配置文件,snakeyaml读取yaml配置文件

下面上我解析yaml文件的例子&#xff1a;// me.yamlage: 1name: jiaobuchongparams:event: whats upurl: http://www.jiaobuchong.comfavoriteBooks:- Gone with the wind- The Little Prince对应的JavaBean类&#xff1a;// Me.javapublic class Me {private Integer age;priv…