2506Tiling(大数递推)

Tiling

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8200   Accepted: 3970

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

Source

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int main()
{
    int a[300][200];
    memset(a,0,sizeof(a));
    a[0][0]=1;
    a[1][0]=1;
    a[2][0]=3;
    for(int i=3; i<=250; i++)
    {
        for(int j=0; j<=100; j++)
        {
            a[i][j]+=(a[i-1][j]+a[i-2][j]+a[i-2][j]);
            if(a[i][j]>=10)
            {
                int t=a[i][j];
                a[i][j]%=10;
                a[i][j+1]+=t/10;
            }
        }
    }
    int n;
    while(cin>>n)
    {
        if(n==0)
            cout<<1<<endl;
        else
        {
            int i=100;
            while(!a[n][i])
            {
                i--;
            }
            for(int j=i; j>=0; j--)
                cout<<a[n][j];
            cout<<endl;
        }

    }
}
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
       Scanner cin = new Scanner ( System.in );
       BigInteger arr[]=new BigInteger[300];
       arr[1]=BigInteger.ONE;
        arr[0]=BigInteger.ONE;
       arr[2]=BigInteger.valueOf(3);
       arr[3]=BigInteger.valueOf(5);
       for(int i=4;i<=270;i++){
    	   arr[i]=arr[i-2].multiply(BigInteger.valueOf(2));
    	   arr[i]=arr[i].add(arr[i-1]);
       }
    	int t;
       while(cin.hasNext()==true){
    	   t=cin.nextInt();
    	   System.out.println(arr[t].toString());
       }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-01 11:25:28

2506Tiling(大数递推)的相关文章

POJ 1737 Connected Graph (大数+递推)

题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS. http://oeis.org/A001187 但不支持这样做.TAT 间接做. 总方案数减去不合法方案. 因为\(n\)个点的完全图有 \(C(n,2)={n(n-1) \over 2}\) 条边,显然就有 \(2^{C(n,2)}\) 种子图,即枚举每条边是否选择. 设$ f[i]$ 表示每个点都和点 \(1\)

UVA 10254-The Priest Mathematician(大数+递推)

题目大意:在原本的汉诺塔游戏基础上加一根柱子,移动策略是:要移动N个汉诺塔,先用4根柱子把K个到一个柱子,然后用其余3根把剩下的N-K个移动到目标柱子,再用4根把初始的K个移动到目标柱子. 关键的问题是找到每个N的K是多少,观察可以发现规律是:随着K的递增,其实移动的次数Fn(K)先递增后递减,然后F1(K),F2(K),...的最大值随着K的增大递增.要形式化证明似乎比较困难..不过在题目的范围内这是可以AC的.程序中维护这个当前使得Fi(K)最大的K,然后递推. import java.ut

1sting 大数 递推

You will be given a string which only contains '1'; You can merge two adjacent '1' to be '2', or leave the '1' there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find th

UVa 10328 Coin Toss(Java大数+递推)

https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具体做法可以参考另一篇博客http://www.cnblogs.com/zyb993963526/p/7203833.html 但是这道题目的话是要用大数来做的,c++感觉不太好写,就学了下java的做法. 1 import java.math.BigInteger; 2 import java.ut

CodeVs 3150 (大数 + 递推)

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<map> #include<set> #include<vector> #include<queue> #include<stack> //#in

poj2506 大数递推

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 3974 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,

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)

ACM学习历程—HDU1041 Computer Transformation(递推 &amp;&amp; 大数)

Description A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, afte

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a