POJ 2506 Tiling (递推 + 大数加法模拟 )

Tiling

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7965   Accepted: 3866

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
算法分析:递推公式:f[i]=f[i-1]+f[i-2]*2; 此公式也不是我自己推导出来的,我也没推导出来,我从ACM之家上的java代码看出的公式。代码:
#include <stdio.h>
#include <string.h>
int a[1001][501]={0};
int main()
{
    int n;
    int i, j, h, e;
    a[0][500] = 1;
    a[1][500] = 1;
    a[2][500] = 3;
    for(i=3; i<=250; i++)
    {
        h = 0;
        for(j=500; j>=0; j--)
        {
            e=a[i-2][j]*2+a[i-1][j]+h;
            a[i][j]=e%10;
            h=e/10;
        }
    }
    while(scanf("%d", &n)!=EOF)
    {
        if(n==0)
        {
            printf("1\n"); continue;
        }
        i = 0;
        while(a[n][i]==0)
        {
            i++;
        }
        for(i; i<=500; i++)
        {
            printf("%d", a[n][i] );
        }
        printf("\n");
    }
    return 0;
}

这还有一份java代码,但是提交WA,不知道哪里出了问题。

import java.util.*;
import java.math.*;
public class Main{
	static BigInteger[] ans; //
	public static void main(String[] args){
		Scanner reader=new Scanner(System.in);
		ans = new BigInteger[251];
		ans[0]=BigInteger.valueOf(1);
		ans[1]=BigInteger.valueOf(2);
		ans[2]=BigInteger.valueOf(3);
		for(int i=3; i<=250; i++)
		{
			ans[i] = ans[i-1].add(ans[i-2].multiply(BigInteger.valueOf(2)));
		}
		int n;
		while(reader.hasNextInt()){
			n=reader.nextInt();
			System.out.println(ans[n]);
		}
	}
}
				
时间: 2024-10-17 10:00:48

POJ 2506 Tiling (递推 + 大数加法模拟 )的相关文章

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

[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(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

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]

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

POJ 2506 Tiling(递推+大整数加法)

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

POJ 2506 Tiling (递推+高精度)

[题目链接]click here~~ [题目大意] In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. [解题思路]: (1)一个2*2的格子有三种填充方法: 两个横着放, 两个竖着放, 放一个2*2 (2)得出递推公式F[i]=F[i-1]+F[i-2]*2 然后就是套高精度模板了 代码; /* Author:HRW 递推+

Tiling 简单递推+大数

Tiling c[0]=1,c[1]=1,c[2]=3;   c[n]=c[n-1]+c[n-2]*2;   0<=n<=250.   大数加法 java  time  :313ms 1 import java.util.*; 2 import java.math.*; 3 public class Main 4 { 5 static int MS=251; 6 static BigInteger[] ans; 7 8 public static void main(String[] args

[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,