Description
小时候,我只能一阶一阶得爬楼梯,
后来,我除了能一次爬一阶,还可以一次爬两阶,
到现在,我最多一次可以爬三阶。
那么现在问题来了,我想爬上n层楼,相邻楼层之间有一段楼梯,虽然我一次可以爬1个台阶、2个台阶和3个台阶,但是我在i与i+1层之间的楼梯上时,我不能跨越到i+1与i+2层之间的楼梯。现在有个n层的楼,知道每一段楼梯的阶数,我想知道,如果我只会往上走,并且忽略其他不在楼梯上的其他移动,共有多少种方案可以到达第n层。
Input
第一行一个整数T(0<T<=50)表示有多少组样例。
对于每一组样例:
第一行一个n(1<n<=50)表示有多少层楼。
接下来一行,包括n-1个整数xi(0<xi<=20),由下到上依次表示每段楼梯的长度。
Output
对于每组数据,输出一行表示共有多少种方案。由于答案较大,所以输出答案请对10007取模。
Sample Input
22344 5 6
Sample Output
42184
动态规划
和杭电上那道上楼梯类似,a[i]表示从i-1层到第i层的方法数,所以s=a[1]*a[2]*a[3]*a[4]*........*a[i;
#include<stdio.h>
#include<functional>
#include<vector>
#include<queue>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#define INF 9999999
using namespace std;
int main()
{
int a[25];
a[1]=1;
a[2]=2;
a[3]=4;
for(int i=4;i<=20;i++)
{
a[i]=a[i-1]+a[i-2]+a[i-3];
a[i]=a[i]%(10007);
}
int n;
scanf("%d",&n);
while(n--)
{
int s=1;
int t;
scanf("%d",&t);
t=t-1;
while(t--)
{
int m;
scanf("%d",&m);
s*=a[m];
s=s%10007;
}
printf("%d\n",s);
}
return 0;
}