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 ;
}

HDU 1250 Hat's Fibonacci(高精度)的更多相关文章

  1. hdu 1250 Hat's Fibonacci(高精度数)

    //  继续大数,哎.. Problem Description A Fibonacci sequence is calculated by adding the previous two membe ...

  2. hdu 1250 Hat's Fibonacci

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...

  3. HDU 1250 Hat's Fibonacci(大数相加)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...

  4. HDU 1250 Hat's Fibonacci (递推、大数加法、string)

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  6. hdu 1250 Hat's Fibonacci(java,简单,大数)

    题目 java做大数的题,真的是神器,来一道,秒一道~~~ import java.io.*; import java.util.*; import java.math.*; public class ...

  7. hdu 1250 Hat&#39;s Fibonacci

    pid=1250">点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding ...

  8. HDU 4099 Revenge of Fibonacci(高精度+字典树)

    题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...

  9. Hat's Fibonacci(大数加法+直接暴力)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 M ...

随机推荐

  1. C++常用的系统函数

    数学<math.h>: 1 三角函数 double sin (double); double cos (double); double tan (double); 2 反三角函数 doub ...

  2. 浅析MySQL主从复制技术(异步复制、同步复制、半同步复制)

      Preface       As we all know,there're three kinds of replication in MySQL nowadays.Such as,asynchr ...

  3. Angular.js进阶

    1.常用指令 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...

  4. 启动pip时,< Fatal error in launcher: Unable to create process using '"' >问题的原因及解决方法

    根本原因 要启动的pip程序,中指定的python程序路径不对 实例分析 我的window电脑上同时安装了python2.7和python3.6,他们的安装路径如下图: 注意图python2.7中红线 ...

  5. PHP的发展历程

    PHP的发展历程 了解一门语言,我们必须知道这门语言的发展史,现在我通过版本的变化以时间轴的形式来说明PHP的发展历程. 1.1995年初PHP1.0诞生 Rasmus Lerdof发明了PHP,这是 ...

  6. Mac进度条卡在100%

  7. 基于CentOS-6.9_x64系统QT环境搭建

    想从事QT开发的人员,首先要做的第一件事就是开发环境的搭建.本人也是一位刚入门的新手,为了搭建这么一个环境,参考了很多的网上教程,然而中间依然走了不少弯路.现将过程记录下来. 一.开发环境    Ce ...

  8. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  9. elasticsearch集群整合elqsticsearch-sql插件

    1.本来整合这个插件是比较简单易操作的,但是由于公司从AWS下载禁掉了,给安装带来一些麻烦, 采用离线安装,先FQ将elasticsearch-sql-5.1.2.0.zip下载下来: ./bin/e ...

  10. 一个例子说明substr(), mb_substr() 和 mb_strcut()之间的区别

    例子来自PHP官方文档,我只是翻译下. http://www.php.net/manual/zh/function.mb-strcut.php header( 'Content-Type:text/h ...