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. MongoDB中的数据导出为excel CSV 文件

    1.打开命令行,进入我们所安装的mongodb路径下的bin文件夹 2.我们采用bin文件夹下的mongoexport方法进行导出, mongoexport -d myDB -c user -f _i ...

  2. git使用教程推荐

    Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是 ...

  3. set_magic_quotes_runtime

    在php的配置文件中,有个布尔值的设置,就是magic_quotes_runtime,当它打开时,php的大部分函数自动的给从外部引入的(包括数据库或者文件)数据中的溢出字符加上反斜线. 当然如果重复 ...

  4. Centos/Fedora下安装Twisted,failed with error code 1 in /tmp/pip-build-H1bj8E/twisted/解决方法

    Python踩坑之路 pip/easy_install无法安装Twisted或者安装后无法导入Twisted 看到MM网站上很多图,想用Scrapy框架爬点图,遇到各种库的问题,蛋疼. 一直twist ...

  5. Java并发编程实战2-线程安全

    1. 定义 一个类是线程安全的,是指在被多个线程访问时,类可以持续进行正确的行为. 2. WHY 我们想要的是线程安全的程序,为什么在线程安全的开始讲线程安全的类呢? 编写线程安全的代码,本质上就是管 ...

  6. UVA10759_Dice Throwing

    求掷骰子n次,点数之和超过m的概率有多大?分数表示. 两种方法: 1.直接DP.用两个数组分别表示分子和分母,注意计算过程中时时约分. 2.将(x1+x2+x3+x4+x5+x6)n多项式展开,把大于 ...

  7. Luogu4884 多少个1?(BSGS)

    11……1(n个)=99……9(n个)/9=(10n-1)/9. 那么显然就是求离散对数了,BSGS即可. #include<iostream> #include<cstdio> ...

  8. BZOJ3244 NOI2013树的计数(概率期望)

    容易发现的一点是如果确定了每一层有哪些点,树的形态就确定了.问题变为划分bfs序. 考虑怎样划分是合法的.同一层的点在bfs序中出现顺序与dfs序中相同.对于dfs序中相邻两点依次设为x和y,y至多在 ...

  9. [POJ1704]Georgia and Bob 博弈论

    从这开始我们来进入做题环节!作为一个较为抽象的知识点,博弈论一定要结合题目才更显魅力.今天,我主要介绍一些经典的题目,重点是去理解模型的转化,sg函数的推理和证明.话不多说,现在开始! Georgia ...

  10. 【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...