递推 + 高精度 --- 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, 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

【题目来源】
The UofA Local 2000.10.14
http://poj.org/problem?id=2506

【题目大意】
意思很简单,给你一块2*n的地板,问你使用1*2、2*2的瓷砖来铺有几种方法。

【题目分析】
看到样例就可以发现是高精度。
首先,我们假设前2*(n-1)块地板已经铺好了,那么我们要铺到2*n就只有一种方法,就是使用1*2这一种瓷砖。
假设前2*(n-2)块地板已经铺好了,那么我们要铺到2*n则有三种方法,也就是使用两块1*2,或者使用两块2*1,或者使用一块2*2,但是这其中有一种是包含在上面的
方法中的,所以在递推的时候只能算2种。
因此我们得到了我们的递推公式:
f(1)=1;
f(2)=1;
f(3)=3;
f(n)=f(n-1)+f(n-2)*2;

剩下的就是实现的问题,高精度简单加法。

ac代码:


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
int ans[260][300];
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
memset(ans,0,sizeof(ans));
ans[0][0]=1;
ans[1][0]=1;
ans[2][0]=3;
if(n<=2)
{
printf("%d\n",ans[n][0]);
}
else
{
int temp=0;
int s=0;
for(i=3;i<=n;i++)
{
for(j=0;j<300;j++)
{
s=ans[i-1][j]+ans[i-2][j]*2+temp;
ans[i][j]=s%10;
temp=s/10;
}
}
int flag=0;
for(i=300;i>=0;i--)
{
if(flag||ans[n][i])
{
flag=1;
printf("%d",ans[n][i]);
}
}
puts("");
}

}
return 0;
}



递推 + 高精度 --- Tiling

时间: 2024-08-05 02:28:23

递推 + 高精度 --- Tiling的相关文章

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 递推+

递推+高精度+找规律 UVA 10254 The Priest Mathematician

题目传送门 1 /* 2 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 3 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子,2 ^ (n - k) - 1 4 所以f[n] = min (f[k] * 2 + g[n-k]),n<=10000,所要要用高精度,另外打表能看出规律 5 */ 6 /************************************************ 7 * Auth

BZOJ 1089 严格n元树 (递推+高精度)

题解:用a[i]表<=i时有几种树满足度数要求,那么这样就可以递归了,a[i]=a[i-1]^n+1.n个节点每个有a[i-1]种情况,那么将其相乘,最后加上1,因为深度为0也算一种.那么答案就是a[n]-a[n-1].然后就是高精度的问题了,发现很久没有现码高精度没手感了,连高进度加法进位都出了些问题,需要特别注意. #include <cstdio> #include <cstring> #include <algorithm> using namespace

[luogu]P1066 2^k进制数[数学][递推][高精度]

[luogu]P1066 2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后,则q的总位数不超过w. 在这里,正整数k(1≤k≤9)和w(k<W≤30000)是事先给定的. 问:满足上述条件的不同的r共有多少个? 我们再从另一角度作些解释:设S是长度为w 的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q

[BZOJ1089][SCOI2003]严格n元树(递推+高精度)

题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1089 分析: 第一感觉可以用一个通式求出来,但是考虑一下很麻烦,不好搞的.很容易发现最底层必有一个是满高度的,其他的任意. 所以直接的递推也不好想. (以下所述都是n元树) 于是可以令f[d]为深度<=d的树的个数,那么深度为d的就是f[d]-f[d-1] 对于深度<=d的又该怎么处理呢? 考虑第一层的n个点(根为0层),每个点都要底下连子树,深度为0~i-1,方案数即f[d-1]

BZOJ 1002 FJOI2007 轮状病毒 递推+高精度

题目大意:轮状病毒基定义如图.求有多少n轮状病毒 这个递推实在是不会--所以我选择了打表找规律 首先执行下面程序 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define M 110 using namespace std; struct abcd{ int to,next; bool ban; }table[M<<2]; int head[

【BZOJ】1089: [SCOI2003]严格n元树(递推+高精度/fft)

http://www.lydsy.com/JudgeOnline/problem.php?id=1089 想了好久的递推式,,,然后放弃了QAQ 神思路!orz 首先我们设$f[i]$表示深度最大为i的n元树的数目,注意,是最大深度为i! 那么易得递推式 f[i]=f[i-1]^n+1 前面表示子树的情况乘积,后面表示树为1层!因为1层是合法的!即没有子女! 然后答案就是 f[d]-f[d-1] !!!为什么要剪掉呢?因为看我们的转移,并不是深度为i,而是深度最大为i,那么为什么要这样减呢?理由

HDU 1041[Computer Transformation] 递推 高精度

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1041 题目大意:初始数字1.规则1变成01,0变成10.问经过N次完整变换后,有多少连续零对. 关键思想:考虑零对的直接来源只有"10",而"10"的直接来源只有"1"或者"00".于是可以得到以下几种递推式 1. dp[i]=pow(2,i-3)+dp[i-2];当前层多少1,下一层就多少10,下下层就多少00:当前层多少00,

Buy the Ticket(卡特兰数+递推高精度)

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i