POJ 2506:Tiling

Tiling

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7452   Accepted: 3639

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.

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
题目大意就是有2×1和2×2两种规格的地板,现要拼2×n的形状,共有多少种情况。
递推思想:
假设我们已经铺好了2×(n-1)的情形,则要铺到2×n则只能用2×1的地板
假设我们已经铺好了2×(n-2)的情形,则要铺到2×n则可以选择1个2×2或两个2×1,故可能有下列三种铺法

其中要注意到第三个会与铺好2×(n-1)的情况重复,故不可取,故可以得到递推式

a[i]=2*a[i-2]+a[i-1];

而且是大数问题。。前面用JAVA水过特别爽。。所以这题也是用JAVA写的。

import java.math.BigInteger;
import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		    int n;
		    BigInteger[] a = new BigInteger[300];
			a[0]=BigInteger.ONE;
			a[1]=BigInteger.ONE;
			a[2]=BigInteger.valueOf(3);
			for(int i=2;i<a.length;i++)
			{
				a[i]=a[i-1].add(a[i-2].multiply(BigInteger.valueOf(2)));
			}
			while (cin.hasNext()){
				n=cin.nextInt();
				System.out.println(a[n]);
			}
	}

}

POJ 2506:Tiling,布布扣,bubuko.com

时间: 2024-10-06 00:40:11

POJ 2506:Tiling的相关文章

poj 2506 Tiling(java解法)

题目链接:http://poj.org/problem?id=2506 本题用的java解的,因为涉及到大数问题,如果对java中的大数操作不熟悉请点这儿:链接 思路:地推公式f[i]=f[i-1]+2*f[i-2] code: import java.math.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(Syst

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 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. Input Input is a sequence of lines,

POJ 2506 -TILING

水题,一个小模拟,规律也好找 f3 = f1 * 2 + f2; #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; const int N = 100; #define ll long long using namespace std; int a[251][N]

POJ 3420 Quad Tiling 题解 《挑战程序设计竞赛》

POJ 3420 Quad Tiling贴瓷砖:4*N的地板上用2*1的瓷砖铺满,求所有方案数对M求余.3.4熟练掌握动态规划矩阵的幂久违地上了节课,太无聊,只好刷一题.假设S[n]表示填满n时的方案数,有S[0]=1.定义矩阵M[p][q] := 边缘p和边缘q可以拼合时取1,否则取0所谓的可以拼合表示,两个边缘拼起来后长度为1(为2就拼接不起来了),且内部缝隙可以用2*1的瓷砖填满.那么M就有一些简单的性质了,比如M的第一行应该是:0 0 0 0 0 0... 继续阅读:码农场 » POJ

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o