首页 > 代码库 > ZOJ 2771

ZOJ 2771


 

Description

Considering a light entering three adjacent planes of glass.

At any meeting surface, the light may either reflect or continue straight through. For example, here is the light bouncing five times before it leaves the glass.

Given the times the light bounces before leaving the glass, you are asked to tell how many different paths the light can take before leaving the glass.

Input:

Input contains serverl test cases, each test case contains only one integer n (0 <= n <= 60) indicates how many bounces the light take.

Output:

Output how many different paths the light can take.

Sample Input:

 

01

 

Sample Output:

 

13

 

Hint:

n = 0, the light leave the glass without any bounces, it go straight through the glass.

n = 1, there are three ways for the light to travel with one bounce--bounce at the middle two meeting faces plus the bottom one.



//关键点在于求出数列的递归式子,这道题吧,其实可以用dp做,这个公式可以由dfs推出来
#include <stdio.h>long long a[70]={1,3,6};int main(){ int i,j,N; while(scanf("%d",&N)!=EOF) { for(i=0;i<=N;i++) if(i>2) { a[i]=a[i-1]+a[i-2]; for(j=i-2;j>=0;j--) a[i]+=a[j]; a[i]+=1; } printf("%lld\n",a[N]); } return 0;}