1049. Mondriaan

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One day, while working on his latest project, he was intrigued by the number of different ways in which he could order several objects to fill an arbitrary region. Expert as he was in this material, he saw at a glance that this was going to be too hard, for there seemed to be innumerable ways to do this. To make his task a little easier, he decided to start with only two kinds of objects: squares with width 1 and height 1, and rectangles with width 2 and height 1. After working on it for half an hour, he knew that even this was too much, for all of his paper was filled with pages like this. The only paper left was his toilet paper, and strange as it now seems, he continued with his task. Fortunately the width of the toilet paper equaled the width of the rectangle, which simplified things a lot. This seemed to do just fine, for in a few minutes time, he produced the following drawing:

             
             

Mondriaan decided to make several of these drawings, each on a piece of toilet paper with a different length. He wanted to give the drawings in his ‘toilet series’ names according to the last digit of the number of ways to fill a piece of toilet paper of a particular length with squares and rectangles. Computers might come in handy in cases like this, so your task is to calculate the name of the drawing, given the length of the toilet paper. The length will be measured in the same dimension as the squares and rectangles.

Input

The input consists of a line containing the number N (1≤N≤100) of drawings in the series. Each consecutive line consists of a number L (0≤L≤1000000) which is the length of the piece of toilet paper used for the drawing.

Output

The output consists of the number that is the name for the corresponding drawing.

Sample Input

5
0
1
2
3
4

Sample Output

1
2
7
2
1

这道题与hdu1992.Tiling a Grid With Dominoes非常类似。也是铺地板的动态规划问题。

注意分析:

1.铺满2*1时,

共有两种情况

2.铺满2*2时,且不和上面情况重复的有3种,

共有3种情况

3.当i >= 3 时,我们又不想与上面的情况重复,那么只有选择突出一个的情况,也就是永远只能靠小正方形来填满的情况:

共有2种情况

因此得到通项:dp[i] = 2*dp[i-1] + 3*dp[i-2] + 2*(dp[i-3]+dp[i-4]+dp[i-5]+......+dp[1] + dp[0])

然后再类比dp[i-1]的式子,消参即可得到简化式子。

#include <iostream>
#include <memory.h>
using namespace std; int dp[1000002];
int main()
{
int i;
dp[0] = 1;
dp[1] = 2;
dp[2] = 7;
for(i = 3;i <= 1000000;i++)
{
dp[i] = (3*dp[i-1] + dp[i-2] - dp[i-3] + 10) % 10; //这里值得注意
}
int N;
cin >> N;
while(N--)
{
int a;
cin >> a;
cout << dp[a] << endl;
}
return 0;
}

soj1049.Mondriaan的更多相关文章

  1. [poj2411] Mondriaan's Dream (状压DP)

    状压DP Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One nigh ...

  2. POJ 题目2411 Mondriaan's Dream(状压DP)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 787 ...

  3. POJ 2411 Mondriaan&#39;s Dream

    状压DP Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 575 ...

  4. POJ2411 Mondriaan's Dream

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  5. 状压DP POJ 2411 Mondriaan'sDream

    题目传送门 /* 题意:一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种? 状态压缩DP第一道:dp[i][j] 代表第i行的j状态下的种数(状态 ...

  6. HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)

    Mondriaan's Dream Problem Description Squares and rectangles fascinated the famous Dutch painter Pie ...

  7. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  8. poj 2411 Mondriaan&#39;s Dream 【dp】

    题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...

  9. POJ2411 Mondriaan's Dream(状态压缩)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15295   Accepted: 882 ...

随机推荐

  1. OSG学习:计算纹理坐标

    在很多时候,直接指定纹理坐标是非常不方便的,如曲面纹理坐标,只有少数的曲面(如圆锥.圆柱等)可以在不产生扭曲的情况下映射到平面上,其他的曲面在映射到表面时都会产生一定程度的扭曲.一般而言,曲面表面的曲 ...

  2. 手动解析Excel获取文件元数据

    工作中有遇到需要获取上传的Excel文件的列明.最大行数.大小等元数据信息.通常做法是通过Apache的POI工具加载文件然后再读取行列进行处理.这种方法很大的弊端就是需要把excel文件加载到内存, ...

  3. 调整Linux的最大文件打开数

    要调整一下Linux的最大文件打开数,否则squid在高负载时执行性能将会很低.另外,在Linux下面部署应用时,有时候会遇上 Socket/File:Can’t open so many files ...

  4. RPM 安装oracle18c 修改字符集的方法

    1. 安装完preinstall 和 oracle 的rpm版本之后 到这个界面 rpm -ivh oracle-database-preinstall-18c-.el7.x86_64.rpm war ...

  5. dividend = Integer.parseInt(args[0])参数问题

    先来一段代码: package yichang; public class MyExceptionTest { public static void main(String[] args) { int ...

  6. C# 字符串多行显示、文本换行

    以textbox为例 ①:先设置textbox的属性Multiline为true ②:组织好显示字符串:FistLine(第一行要显示的字符).SecondLine(第二行要显示的字符)....... ...

  7. Android 去掉标题全屏显示

    自己测试时出现无法实现去掉标题和全屏功能.最后发现只要public class SocketActivity extends Activity {} 而不能用ActionBarActivity. 先介 ...

  8. 调用webservice超时问题的解决[转]

    1.web.config配置,<system.web></system.web>里面增加:<httpRuntime maxRequestLength="1024 ...

  9. Redis 基础:Redis 数据类型

    Redis 数据类型 Redis支持五种数据类型:string(字符串).hash(哈希).list(列表).set(集合)及zset(sorted set:有序集合). String(字符串) st ...

  10. Pscp与服务器文件传递

    1. 下载pscp.exe https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html 选择并下载pscp.exe 2. 执行cmd程 ...