Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10753 | Accepted: 4996 |
Description
In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles?
Here is a sample tiling of a 2x17 rectangle.
Here is a sample tiling of a 2x17 rectangle.
Input
Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.
Output
For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle.
Sample Input
2 8 12 100 200
Sample Output
3 171 2731 845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
private static Scanner in;
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
BigInteger a[]=new BigInteger[300];//新建对象
a[0]=BigInteger.valueOf(1);//2*0的时候,只要一种情况
a[1]=BigInteger.valueOf(1);//2*1的时候,只有一种情况
a[2]=BigInteger.valueOf(3);//2*2的时候,有三种情况(其中一种与上一个重复)
for(int i=3;i<300;i++)
a[i]=a[i-1].add(a[i-2].multiply(BigInteger.valueOf(2)));//递推得公式:a[i]=a[i-1]+a[i-2]*2
Scanner in=new Scanner(System.in);//扫描从控制台输入的字符
while(in.hasNext())//等价于!=EOF
{
n=in.nextInt();//读入一个int型的数
// System.out.println(a[in.nextInt()]);//从输入流读取下一个整数
System.out.println(a[n]);
}
}
}