soj1049.Mondriaan

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;
}

 

转载于:https://www.cnblogs.com/sysu-blackbear/p/3280001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值