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. C1WPF制作OLAP Cube浏览工具

    经过前期一段时间对WPF的学习了解,相信大家对WPF有了一定的了解.今天我们一起来了解使用Component One(简称C1)的WPF控件制作CUBE浏览工具.其实这个OLAP控件官方已经有了很详细 ...

  2. Effective Modern C++翻译(2)-条款1:明白模板类型推导

    第一章 类型推导 C++98有一套单一的类型推导的规则:用来推导函数模板,C++11轻微的修改了这些规则并且增加了两个,一个用于auto,一个用于decltype,接着C++14扩展了auto和dec ...

  3. 分离链表法散列ADT

    分离链表法解决冲突的散列表ADT实现 数据结构定义如下: struct ListNode; typedef struct ListNode *Position; struct HashTbl; typ ...

  4. Window环境下搭建Vue.js开发环境

    原文链接:http://blog.csdn.net/solo95/article/details/72084907 笔者最近在进行前端的学习,在点完了HTML5.CSS3.JavaScript等技能树 ...

  5. hdu 5475 (线段树)

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 学习Spring Boot:(四)应用日志

    前言 应用日志是一个系统非常重要的一部分,后来不管是开发还是线上,日志都起到至关重要的作用.这次使用的是 Logback 日志框架. 正文 Spring Boot在所有内部日志中使用Commons L ...

  7. BZOJ 1014 火星人 | 平衡树维护哈希

    BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...

  8. BZOJ4589 Hard Nim 【FWT】

    题目链接 BZOJ4589 题解 FWT 模板题 #include<algorithm> #include<iostream> #include<cstdlib> ...

  9. BZOJ4416 [Shoi2013]阶乘字符串 【序列自动机 + 状压dp】

    题目链接 BZOJ4416 题解 建立序列自动机,即预处理数组\(nxt[i][j]\)表示\(i\)位置之后下一个\(j\)出现的位置 设\(f[i]\)表示合法字符集合为\(i\)的最短前缀,枚举 ...

  10. 洛谷 P4009 汽车加油行驶问题 解题报告

    P4009 汽车加油行驶问题 题目描述 给定一个\(N×N\)的方形网格,设其左上角为起点◎,坐标(1,1) ,\(X\)轴向右为正,\(Y\)轴向下为正,每个方格边长为1 ,如图所示. 一辆汽车从起 ...