HDU 2606 +HDU 2151

hdu 2606 Renovation Problem(递推+思维)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 759    Accepted Submission(s): 265


 

Problem Description

The New Year’s Day is comming ! Every family want to make theiir house looks more beautiful , so does Teddy’s . To make his own house looks different from other's , he went to the shop to buy four kinds of tiles . each kind of tiles has different size : 1*1 , 2*2 , 3*3 and 4 * 4, respectively .


The problem is , how many ways there are to tile his floor which has a area of 4*N using these four kinds of tiles ? You can assume that the number of each kind of tiles is enough.

Input

The first line contain a T. followed by T lines ,each line contain a integer N.(1<=N <=100).

Output

For each case, output the ans % 19890907.

Sample Input

2

1

2

Sample Output

1

5

Author

Teddy

Source

HDU 1st “Vegetable-Birds Cup” Programming Open Contest

【分析】

递推。设f[i]是前i列填满的情况。那么,

  • f[i-1] 前i-1列铺满,剩下1列,4×1的空间只能用1×1的来铺,只有1种铺法;
  • f[i-2] 前i-2列铺满,剩下2列,4×2的空间可以用2×2的了,这时必须要用上2×2的,因为如果只用1×1,和前面就重复了。所以,有4种铺法;
  • 同理,f[i-3],有2种;f[i-4]只有一种
  • 同时,i-3的时候,会有另一种情况,就是3列做一个整体,如下图:用2×2和1×1;
  • 1 1 0
    1 1 0
    0 1 1
    0 1 1
  • 所以得到递推公式:f[n]=f[n-1]*1+f[n-2]*4+f[n-3]*2+f[n-4]*1+{  特殊考虑:(f[n-3]*2) }

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
const int mod=19890907;
int f[maxn];
int main()
{
        memse(f,0,sizeof(f));
	f[0]=1;
	f[1]=1;
	f[2]=5;
	f[3]=13;
	for(int i=4;i<maxn;i++)
	{
		f[i]=(f[i-1]+f[i-2]*4+f[i-3]*2+f[i-4])%mod;
		for(int j=3;j<=i;j++)    //  特殊处理
			f[i]=(f[i]+f[i-j]*2)%mod;
	}
	int t;scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		printf("%d\n",f[n]);
	}
    return 0;
}

hdu 2151 Worm(dp)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4795    Accepted Submission(s): 3046


 

Problem Description

自从见识了平安夜苹果的涨价后,Lele就在他家门口水平种了一排苹果树,共有N棵。

突然Lele发现在左起第P棵树上(从1开始计数)有一条毛毛虫。为了看到毛毛虫变蝴蝶的过程,Lele在苹果树旁观察了很久。虽然没有看到蝴蝶,但Lele发现了一个规律:每过1分钟,毛毛虫会随机从一棵树爬到相邻的一棵树上。

比如刚开始毛毛虫在第2棵树上,过1分钟后,毛毛虫可能会在第1棵树上或者第3棵树上。如果刚开始时毛毛虫在第1棵树上,过1分钟以后,毛毛虫一定会在第2棵树上。

现在告诉你苹果树的数目N,以及毛毛刚开始所在的位置P,请问,在M分钟后,毛毛虫到达第T棵树,一共有多少种行走方案数。

Input

本题目包含多组测试,请处理到文件结束(EOF)。
每组测试占一行,包括四个正整数N,P,M,T(含义见题目描述,0<N,P,M,T<100)

Output

对于每组数据,在一行里输出一共的方案数。
题目数据保证答案小于10^9

Sample Input

3 2 4 2

3 2 3 2

Sample Output

4

0

Hint

第一组测试中有以下四种走法:

2->1->2->1->2

2->1->2->3->2

2->3->2->1->2

2->3->2->3->2

【分析】仔细看题。题上说的是,每过一分钟,蚂蚁会爬到相邻的树上 。

【代码】

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int dp[105][105];
int main()
{
    int n,p,m,t;
    while(~scanf("%d%d%d%d",&n,&p,&m,&t))
    {
        memset(dp,0,sizeof(dp));
        dp[0][p]=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
                dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1];
        }
        printf("%d\n",dp[m][t]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/83037599
hdu