HDU 1250 Hat's Fibonacci(高精度)

时间:2024-06-15 16:03:08
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.

题目大意:看题。
思路:高精度

代码(671MS):

 //模板测试
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; const int MAXN = ; struct bign {
int len, s[MAXN]; bign () {
memset(s, , sizeof(s));
len = ;
}
bign (int num) { *this = num; }
bign (const char *num) { *this = num; } bign operator = (const int num) {//数字
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char *num) {//字符串
for(int i = ; num[i] == ''; num++) ; //去前导0
if(*num == ) --num;
len = strlen(num);
for(int i = ; i < len; ++i) s[i] = num[len-i-] - '';
return *this;
} bign operator + (const bign &b) const {
bign c;
c.len = ;
for(int i = , g = ; g || i < max(len, b.len); ++i) {
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
} bign operator += (const bign &b) {
*this = *this + b;
return *this;
} void clean() {
while(len > && !s[len-]) len--;
} bign operator * (const bign &b) {
bign c;
c.len = len + b.len;
for(int i = ; i < len; ++i) {
for(int j = ; j < b.len; ++j) {
c.s[i+j] += s[i] * b.s[j];
}
}
for(int i = ; i < c.len; ++i) {
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator *= (const bign &b) {
*this = *this * b;
return *this;
} bign operator - (const bign &b) {
bign c;
c.len = ;
for(int i = , g = ; i < len; ++i) {
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= ) g = ;
else {
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b) {
*this = *this - b;
return *this;
} bign operator / (const bign &b) {
bign c, f = ;
for(int i = len - ; i >= ; i--) {
f *= ;
f.s[] = s[i];
while(f >= b) {
f -= b;
c.s[i]++;
}
}
c.len = len;
c.clean();
return c;
}
bign operator /= (const bign &b) {
*this = *this / b;
return *this;
} bign operator % (const bign &b) {
bign r = *this / b;
r = *this - r*b;
return r;
}
bign operator %= (const bign &b) {
*this = *this % b;
return *this;
} bool operator < (const bign &b) {
if(len != b.len) return len < b.len;
for(int i = len-; i >= ; i--) {
if(s[i] != b.s[i]) return s[i] < b.s[i];
}
return false;
} bool operator > (const bign &b) {
if(len != b.len) return len > b.len;
for(int i = len-; i >= ; i--) {
if(s[i] != b.s[i]) return s[i] > b.s[i];
}
return false;
} bool operator == (const bign &b) {
return !(*this > b) && !(*this < b);
} bool operator != (const bign &b) {
return !(*this == b);
} bool operator <= (const bign &b) {
return *this < b || *this == b;
} bool operator >= (const bign &b) {
return *this > b || *this == b;
} string str() const {
string res = "";
for(int i = ; i < len; ++i) res = char(s[i]+'') + res;
return res;
}
}; istream& operator >> (istream &in, bign &x) {
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign &x) {
out << x.str();
return out;
} bign f[]; void solve(int n) {
f[] = f[] = f[] = f[] = ;
if(n < ) cout<<f[n]<<endl;
else {
int x = ;
for(int i = ; i <= n; ++i) {
f[] = f[] + f[] + f[] + f[];
f[x] = f[];
if(++x == ) x = ;
}
cout<<f[]<<endl;
}
} int main() {
int n;
while(scanf("%d", &n)!=EOF) {
solve(n);
}
return ;
}