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 ...
随机推荐
- 【博弈论】Road to Arabella Gym - 102263B
题目: 题目大意:输入n,k.两个人轮流选一个数x(1<=x<=max(1,n-k))减去n,若到一个人的回合n=0那么那个人失败.Kilani先手. 通过手动模拟几个实例,很容易发现先手 ...
- Python基础知识点:多进程的应用讲解
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:东哥IT笔记 现在很多CPU都支持多核,甚至是手机都已经开始支持多核 ...
- django-rest-framework-源码解析003-视图家族和路由(APIView/GenericAPIView/mixins/generics/viewsets)
视图家族 视图家族在rest_framework源码位置和学习曲线为: rest_framework.views: 基本视图(APIView) rest_framework.generics: 工具视 ...
- spring oauth2+JWT后端自动刷新access_token
这段时间在学习搭建基于spring boot的spring oauth2 和jwt整合. 说实话挺折腾的.使用jwt做用户鉴权,难点在于token的刷新和注销. 当然注销的难度更大,网上的一些方案也没 ...
- 小书MybatisPlus第8篇-逻辑删除实现及API细节精讲
本文为Mybatis Plus系列文章的第8篇,前7篇访问地址如下: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总 ...
- Kafka入门(2):消费与位移
摘要 在这篇文章中,我将从消息在Kafka中的物理存储方式讲起,介绍分区-日志段-日志的各个层次. 然后我将接着上一篇文章的内容,把消费者的内容展开讲一讲,区分消费者与消费者组,以及这么设计有什么用. ...
- 【扩展推荐】Intervention/image 图片处理
Intervention/image 是为 Laravel 定制的图片处理工具, 它提供了一套易于表达的方式来创建.编辑图片. 一.环境要求 二.安装及配置 下载地址:https://packagis ...
- Redis之NoSql入门和概述(二)
2. 什么是NoSQL? 2.1 NoSQL 概述 NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库.随着互联网web2.0网站的兴起, ...
- Kaggle 入门题-泰坦尼克号灾难存活预测
这个题目的背景概况来讲就是基于泰坦尼克号这个事件,然后大量的人员不幸淹没在这个海难中,也有少部分人员在这次事件之中存活,然后这个问题提供了一些人员的信息如姓名.年龄.性别.票价,所在客舱等等一些信息, ...
- Python for循环通过序列索引迭代
Python for 循环通过序列索引迭代: 注:集合 和 字典 不可以通过索引进行获取元素,因为集合和字典都是无序的. 使用 len (参数) 方法可以获取到遍历对象的长度. 程序: strs = ...