您的位置:首页 > 其它

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

2015-07-15 20:54 393 查看
链接: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.



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
[j]);
        	}
        	 
            System.out.println(ans);
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: