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 ...
随机推荐
- 《Head First 设计模式》:工厂方法模式
正文 一.定义 工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. PS:在设计模式中,"实现一个接口"泛指实现某个超类型(可 ...
- [redis] -- 为什么那么快
1 完全基于内存 2 数据结构简单,对数据操作也简单 3 只有单线程,避免了不必要的上下文切换,也不存在竞态,不存在多进程或多线程导致的切换而消耗CPU,不用开了各种锁的问题 4 使用多路IO复用模型 ...
- BUUCTF-web HappyCTFd (CVE-2020-7245)
在 CTFd v2.0.0 - v2.2.2 的注册过程中,如果在CTFd的用户名和emails可用,则可以使攻击者接管任意账号. 进入题目,进行注册.查看用户可以看到admin账号,利用漏洞获取ad ...
- 一周一个中间件-ES搜索引擎
---toc: truetitle: 一周一个中间件-ES搜索引擎date: 2019-09-19 18:43:36tags: - 中间件 - 搜索引擎--- ## 前言 > 在众多搜索引擎中, ...
- JSONObject遍历
导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...
- laravel 上线部署最佳实践
nginx 配置 listen 80 default_server; server_name xxxx; index index.php index.html; 优先 index.php ro ...
- pandas_处理异常值缺失值重复值数据差分
# 处理异常值缺失值重复值数据差分 import pandas as pd import numpy as np import copy # 设置列对齐 pd.set_option("dis ...
- 从键盘输入一个字符串(长度不超过30),统计字符串中非数字的个数,并将统计的结果显示在屏幕上,用EXE格式实现。
问题 从键盘输入一个字符串(长度不超过30),统计字符串中非数字的个数,并将统计的结果显示在屏幕上,用EXE格式实现. 源程序 data segment hintinput db "plea ...
- Python File fileno() 方法
概述 fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作.高佣联盟 www.cgewang.com 语法 fileno() ...
- Python time asctime()方法
描述 Python time asctime() 函数接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07 ...