POJ2806 Square
题目描述
给定\(2*1\)和\(2 * 2\)两种规格的地砖,请问\(2 * n\)的地面总共有多少种方法?
下面是铺满\(2*17\)的地面的示意图。

输入输出格式
输入
多组数据,每组数据包括1行1个整数n,表示地面的长度。\((0\leq n \leq250)\)
输出
每组数据输出\(1\)行\(1\)个整数,表示铺满\(n\)米地面的方法数。
思路
因为我们知道长度为\(i\)的矩阵只能由\(i-1\)和\(i-2\)变来,又长度为一的矩阵的方法数为\(1\),长度为二的矩阵的方法数为\(3\),其中有一种相当于\(i-1\)的方法数,所以状态转移方程为\(dp[i][j]=dp[i-1][j]+dp[i-2][j]*2;\)
代码
#include<bits/stdc++.h>
using namespace std;
int dp[301][501]; //dp[i]用来存储第i列的结果,dp[i][0]存储长度
int main(){
dp[1][0]=1;
dp[1][1]=1;
dp[2][0]=1;
dp[2][1] = 3;
for(int i=3;i<=300;i++){
int len=max(dp[i-2][0],dp[i-1][0]);
for(int j=1;j<=len;j++)
dp[i][j]=dp[i-1][j]+dp[i-2][j]*2; //按位加
dp[i][0]=max(dp[i-2][0],dp[i-1][0]); //取两个加数中较长的长度
for(int j=1;j<=dp[i][0];j++){
dp[i][j+1]+=dp[i][j]/10; //进位
dp[i][j]%=10;
}
while(dp[i][dp[i][0]+1]>0){ //更新高精度加法结果的位数
dp[i][0]++;
dp[i][dp[i][0]+1]+=dp[i][dp[i][0]]/10;
}
}
int n;
while(cin>>n){
if(n==0)
cout<<1<<endl;
else{
for(int i=dp[n][0];i>=1;i--)
cout<<dp[n][i];
cout<<endl;
}
}
return 0;
}
POJ2806 Square的更多相关文章
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- OPEN CASCADE Gauss Least Square
OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...
- OpenCascade Eigenvalues and Eigenvectors of Square Matrix
OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...
- Leetcode: Matchsticks to Square && Grammar: reverse an primative array
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- Modified Least Square Method and Ransan Method to Fit Circle from Data
In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...
随机推荐
- vue-methods三种调用的形势
var btn = { template:`<button>组件add</button>` } var any = new Vue({ el: '#app', data:{ a ...
- spring学习(二)spring容器搭建与应用
本文只是对spring容器进行操作 最简单的一个案例: 一.导包 使用IDEA的maven工程 1.在pom.xml中导入依赖 <?xml version="1.0" enc ...
- Python数据类型-str,list常见操作
一.字符串操作 语法:字符串名.startwith('字符串') 功能:判断字符串里是否以xxx开头 范例: 扩展:从控制台接收输入居住地址,如果地址以北京市开头,则输出北京人口,否则输入非北京人口. ...
- 究竟什么时候该使用MQ?
究竟什么时候该使用MQ? 原创: 58沈剑 架构师之路 昨天 任何脱离业务的组件引入都是耍流氓.引入一个组件,最先该解答的问题是,此组件解决什么问题. MQ,互联网技术体系中一个常见组件,究竟什么时 ...
- Mysql Backup
1.完全备份# innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/如果要使用一个最小权限的用户进行备份,则可基于 ...
- python map() filter() reduce()函数的用法以及实例
map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...
- JVM详解之:汇编角度理解本地变量的生命周期
目录 简介 本地变量的生命周期 举例说明 优化的原因 总结 简介 java方法中定义的变量,它的生命周期是什么样的呢?是不是一定要等到方法结束,这个创建的对象才会被回收呢? 带着这个问题我们来看一下今 ...
- Python字典内置函数和方法
Python字典内置函数和方法: 注:使用了 items.values.keys 返回的是可迭代对象,可以使用 list 转化为列表. len(字典名): 返回键的个数,即字典的长度 # len(字典 ...
- PHP array_unique() 函数
实例 移除数组中重复的值: <?php$a=array("a"=>"red","b"=>"green" ...
- PHP zip_entry_open() 函数
定义和用法 zip_entry_open() 函数打开一个 zip 档案以供读取.高佣联盟 www.cgewang.com 如果成功,该函数则返回 TRUE.如果失败,则返回 FALSE. 语法 zi ...