题目描述

给定\(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的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. [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 ...

  5. OPEN CASCADE Gauss Least Square

    OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...

  6. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

    OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...

  7. 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 ...

  8. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. 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 ...

随机推荐

  1. IDEA添加注释常用的快捷键

    1.行注释Ctrl+/ 2.块注释Ctrl+Shift+/ 3.生成类注释 输入/**,然后按回车 (idea上没有生成类注释快捷键的,可以看这里 :idea生成类注释和方法注释的正确方法 ) 4.生 ...

  2. elementUI form表单验证不通过的三个原因

    <el-form :model="form" :rules="rules"> <el-form-item prop="input&q ...

  3. WPF入门教程(一)---基础

    这篇主要讲WPF的开发基础,介绍了如何使用Visual Studio 2013创建一个WPF应用程序. 首先说一下学习WPF的基础知识: 1) 要会一门.NET所支持的编程语言.例如C#. 2) 会一 ...

  4. List接口(动态数组)

    List接口(动态数组) List集合类中元素有序且可重复 ArrayList(重要) 作为List接口的主要实现类 线程不安全的,效率高 底层使用Object[] elementData数组存储 A ...

  5. 不看会后悔系列之idea的使用小技巧

    虽然用idea已多达N年,但你对其所有的功能都了如指掌吗?了解如下小tips助你开发更通畅. 调试专题 (1)不用每次都重启debugdebug程序时,只修改了一点代码,怎么在不重启程序的前提下,看到 ...

  6. 7-Pandas之索引调整方法

    一.调整索引.修改列标签 1.调整索引的两种情况: 重新索引 设置新的索引 (1)重新索引 在Pandas对象中,其实索引也是一个对象,所以可对其进行修改. 例如:df.index=['a','b', ...

  7. PHP array_fill() 函数

    ------------恢复内容开始------------ 实例 用给定的键值填充数组: <?php$a1=array_fill(3,4,"blue");print_r($ ...

  8. JavaScript 你真的了解this指向吗

    JavaScript 你真的了解this指向吗 前言 终于开始写this指向了,相信这对很多JavaScript的学习者来说是一个非常恐怖的环节,个人认为也算是JavaScript中最难理解的一个知识 ...

  9. react ts redux-saga | 谷歌Chrome浏览器风格的标签组件 | 中台

    谷歌Chrome浏览器风格的标签组件 选用技术 react typescript redux-saga存储本地标签数据 umi 实现 [x] 支持全部关闭,当前关闭,关闭其他Tab [x] 支持Tab ...

  10. RabbitMQ学习总结(3)-集成SpringBoot

    1. pom.xml引用依赖 SpringBoot版本可以自由选择,我使用的是2.1.6.RELEASE,使用starter-web是因为要使用Spring的相关注解,所以要同时加上. <dep ...