Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8761 | Accepted: 4209 |
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.

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
分析:当铺垫的是ans[i]时,无非两种情形:ans[i-1]则只能在该基础上+2*1大小的砖块
ans[i-2]则在该基础上+两块2*1的或一块2*2的
因而得到递推公式,然而从样例中可以看到。
这题还需要高精度计算,套刘爷的模板吧。
code:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include <deque>
#include <vector>
using namespace std;
struct Bigint {
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int> s;
Bigint(long long num = 0) { *this = num; } //构造函数
Bigint operator = (long long num) {
s.clear();
do {
s.push_back(num%BASE);
num /= BASE;
} while (num > 0);
return *this;
}
Bigint operator =(const string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(0, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
}
Bigint operator + (const Bigint& b) const {
Bigint c;
c.s.clear();
for (int i = 0, g = 0;; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size()) x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x%BASE);
g = x / BASE;
}
return c;
}
Bigint operator * (const Bigint& b) const {
Bigint c;
c.s.clear();
int x = 1;
for (int i = 0, g = 0;; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
if (i < s.size()) x *= s[i];
if(i < b.s.size()) x *= b.s[i];
c.s.push_back(x%BASE);
g = x / BASE;
}
return c;
}
};
ostream& operator << (ostream &out, const Bigint& x)
{
out << x.s.back();
for (int i = x.s.size() - 2; i >= 0; i--)
{
char buf[20];
sprintf(buf, "%08d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) out << buf[j];
}
return out;
}
istream& operator >> (istream &in, Bigint& x)
{
string s;
if (!(in >> s)) return in;
x = s;
return in;
}
int main(void)
{
Bigint ans[255];
ans[0] = 1;
ans[1] = 1;
//cout << ans[0] * ans[1] << endl;
for (int i = 2; i <= 250; i++)
{
ans[i] = ans[i-2]+ans[i-2] + ans[i-1];
}
int n;
while (cin >> n)
{
cout << ans[n] << endl;
}
}