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. php-预定义

    php预定义异常 Exception是所有异常的基类 属性 message:异常消息内容 code:异常代码 file:抛出异常的文件名 line:抛出异常在该文件的行号 ErrorException ...

  2. QT 防止FTP 上传软件在断连处 Crash

    前段时间发现项目中的上传FTP软件有可能会在从服务器申请断连时Crash, 所以加了一个Timer. 由于项目代码行数过大, 此处上传部分代码片段. timeoutTimer = new QTimer ...

  3. NPOI读取Excel遇到的坑

    NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...

  4. 第6章 HDFS HA配置

    目录 6.1 hdfs-site.xml文件配置 6.2 core-site.xml文件配置 6.3 启动与测试 6.4 结合ZooKeeper进行自动故障转移 在Hadoop 2.0.0之前,一个H ...

  5. 笔记-jinja2语法

    笔记-jinja2语法 1.      基本语法 控制结构 {% %} 变量取值 {{ }} 注释 {# #} 2.      变量 最常用的是变量,由Flask渲染模板时传过来,比如上例中的”nam ...

  6. 理解C指针: 一个内存地址对应着一个值

    一个内存地址存着一个对应的值,这是比较容易理解的. 如果程序员必须清楚地知道某块内存存着什么内容和某个内容存在哪个内存地址里了,那他们的负担可想而知.    汇编语法对“一个内存地址存着一个对应的数” ...

  7. python通过mongoengine中connect函数连接多个数据库

    mongoengine支持程序同时连接多个数据库,这些数据库可以位于一个或多个mongo之中,通过alias名称区分不同的连接即可. 可以通过switch_db切换到不同的数据库,进行读写操作,swi ...

  8. 北京Uber优步司机奖励政策(4月4日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 武汉Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 只需两步,rails支持CSV格式导出

    一.Controller最上方添加 require 'csv' 二.方法里面添加 format.csv do csv_string = CSV.generate do |csv| csv <&l ...