UVA10918 - Tri Tiling(递推)

题目链接

题目大意:用2?1的瓷砖去铺3?n的框,问有多少种方式。

解题思路:首先n是奇数是无解的。这题应该说是递推,应该先明白这个状态可以由前面的哪些状态推来,画图可以发现n的状态可以由n - 2这个状态?3得到,还可以由n
- 4这个状态?2得到等等。所以,f(n)
= 3f(n - 2) + 2 f(n - 4) + .. + 2 f(0);再写出f(n - 2)的递推式,相减可以得到化简公式:f(n) = 4
f(n - 2) - f(n - 4),边界f(0) = 1,f(2) = 3.

代码:

#include <cstdio>
#include <cstring>

const int maxn = 35;
typedef long long ll;
ll num[maxn];

void init() {

    num[0] = 1;
    num[2] = 3;

    for (int i = 4; i <= maxn - 5; i += 2)
        num[i] = 4 * num[i - 2] - num[i - 4];
}

int main () {

    init();

    int n;
    while (scanf ("%d", &n) && n != -1) {

        printf ("%lld\n", num[n]);
    }
    return 0;
}
时间: 2024-10-03 16:57:20

UVA10918 - Tri Tiling(递推)的相关文章

HDU1143 Tri Tiling 递推

#include<stdio.h> int main() { int f[31]; int i,L; int n; f[0]=1; for(i=1;i<=30;i+=2) f[i]=0; for(i=2;i<=30;i+=2) { f[i]=0; for(L=2;L<=i;L+=2) { if(L==2) f[i]+=3*f[i-L]; else f[i]+=2*f[i-L]; } } while(scanf("%d",&n),n!=-1) pri

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不是零种

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,

HDU 1143 Tri Tiling (递推)

Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2118    Accepted Submission(s): 1211 Problem Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sa

HDU 1143 Tri Tiling(递推)

题意:现有一些1*2的小方块,求拼成3*n的矩形有多少种拼法. 思路: 既然是递推式,肯定要遇上一层发生关系.仔细观察,发现每一层应该设为2层,(奇数层不可能是矩形)而从上一次拼好的图形中的最后一层可以发现,只有两种结果(对称的也先算一种). 即:.结果二可以==上一层的结果一和结果二两种结果(很明显,不多说,用笔画一下便知).结果一可以==2*(上一层的结果一和结果二)以及结果一.为什么呢?上一层的两种结果都可以分别衍生出1和2两种情况,即2*(上一层的结果一和结果二),而如果上一层为结果一的

递推 + 高精度 --- Tiling

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

[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 (递推+高精度)

[题目链接]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 递推+

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