1815: The Smart Bunny
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() | stdin/stdout | 5s | 8192K | 481 | 256 | Standard |
2nd JOJ Cup Online VContest Problem
We all know that bunny is fond of carrots. One cloudy day, he was told that there would be a violenting flood coming soon to destroy the forests. He was scared and after not thinking too much he told himself that he had to escape. He suddenly recalled that there was a temple on the top of the hill and he might shelter there until the flood's past. But unfortunately there was no food for him on the top of the hill, so he had to take his carrots away along with himself. Then he moved to the foot of the hill and stopped. There was only one way for him to get the top of the hill, that is, a long staircase. Given the number of the steps of the staircase, he asked himself:"how many different ways of strides are there for him to get the top of the hill?". Of course, because of his height, he could only stride a limited range of steps. He was smart so much so that he got the answer quickly. Do you know how he did it?
Input Specification
The input consists of several test cases, each of which occupies a line containing M(1<=M<=40) and N(1<=N<=10), where M indicates the number of the steps of the staircase and N indicates the maximal number of steps the bunny can stride once.
Output Specification
Each test case should correspond to a line in the output. Your program should print an integer which is the answer.
Sample Input
4 2
5 4
Sample Output
5
15
#include<stdio.h>
int main()
{
int n,m,i,j;
while(scanf("%d%d",&m,&n)==2)
{
int a[60]={0};
a[10]=a[11]=1;
for(i=12;i<60;i++)
for(j=1;j<=n;j++)
a[i]+=a[i-j];
printf("%d/n",a[m+10]);
}
return 0;
}
/*算法思想:DP解决。arr[i][j] :i 代表剩余阶梯数,j 代表一次可以走的阶梯数,
动态规划为:arr[i][j] = arr[i-1][j] + arr[i-2][j] + ... ... + arr[i-j][j] ,
当然有些需要特殊处理:arr[0][j] = arr[1][j] = arr[i][1] = 1;
if(j > i) arr[i][j] = arr[i][i] ... ...(*/
#include<iostream>
#define M 41
#define N 11
using namespace std;
int main()
{
int a[M][N],i,j,t,m,n;
for(j=0;j<N;j++)
{
a[0][j]=1;
a[1][j]=1;
}
for(i=0;i<M;i++)
a[i][1]=1;
for(i=2;i<M;i++)
{
for(j=2;j<N;j++)
{
if(j>i)
a[i][j]=a[i][i];
else
{
a[i][j]=0;
for(t=i-1;t>=i-j;t--)
a[i][j]+=a[t][j];
}
}
}
while(cin>>m>>n)
cout<<a[m][n]<<endl;
return 0;
}