ZOJ 2771 Get Out of the Glass 很普通的计数dp

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2771

Get Out of the Glass Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

ZOJ 2771 Get Out of the Glass 很普通的计数dp_第1张图片

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:

0
1

Sample Output:

1
3

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. 



题意:

玻璃一共有3层

从上方射入玻璃后(不能在最上面直接反射出去,必须先进入玻璃),问n次折射的走法有多少种。

dp[i][j]

 i代表第几次折射  奇偶可以判断向上走还是向下。

j代表当前在第几层。


虽然c++也能过  但是其实会爆 llu的。


import java.util.Scanner;
import java.math.*;
import java.text.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        BigInteger []f = new BigInteger[1010];
        BigInteger ans;
        BigInteger[][] dp=new BigInteger[70][4];
        BigInteger li=BigInteger.ZERO;
        BigInteger yi=BigInteger.ONE;
        while(cin.hasNext())
        {
        	int n;
            n=cin.nextInt();
        	for(int i=0;i<=n;i+=1)
        	{
        		for(int j=0;j<4;j+=1)
        		{
        			dp[i][j]=li; 
        		}
        	}
        	dp[0][0]=yi;
        	ans=li;
        	for(int i=0;i<n;i+=1)
        	{
        		for(int j=0;j<4;j+=1)
        		{
        			if(i%2==1)
        			{
        				for(int l=0;l<j;l++)
        					dp[i+1][l]=dp[i+1][l].add(dp[i][j]); 
        			}
        			else
        			{
        				for(int l=j+1;l<4;l++)
        				{
        					dp[i+1][l]=dp[i+1][l].add(dp[i][j]);
        				}
        			}
        			
        		}
        	}
        	
        	
        	for(int j=0;j<4;j++)
        	{
        		ans=ans.add(dp[n][j]);
        	}
        	 
            System.out.println(ans);
        }
    }
}




你可能感兴趣的:(dp)