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. Dubbo 安装ZooKeeper环境

    一.在Windows 安装ZooKeeper 1.下载ZooKeeper 2.解压,修改ZooKeeper配置文件 复制一份zoo_sample.cfg文件,改名位zoo.cfg,打开编辑,设置数据保 ...

  2. QP总体结构

    QP是一个基于事件驱动的嵌入式系统软件框架,其总体结构如下图. AO活动对象由事件队列和层次状态机两部分组成,每个AO占有一个优先级: QF量子框架由五个数据结构及操作组成,其数据结构采用了uCOS- ...

  3. openwrt procd启动流程和脚本分析

    Linux内核执行start_kernel函数时会调用kernel_init来启动init进程,流程如下图: graph LR A[start_kernel] -->B(rest_init) B ...

  4. centos7下使用n grok编译服务端和客户端穿透内网

    (发现博客园会屏蔽一些标题中的关键词,比如ngrok.内网穿透,原因不知,所以改了标题才能正常访问,) 有时候想在自己电脑.路由器或者树莓派上搭建一些web.vpn等服务让自己用,但是自己的电脑一般没 ...

  5. Python爬虫系列 - 初探:爬取新闻推送

    Get发送内容格式 Get方式主要需要发送headers.url.cookies.params等部分的内容. t = requests.get(url, headers = header, param ...

  6. ruby配置镜像源

    1.打开电脑的cmd窗口,输入如下命令即可查看gem镜像: gem sources l 或是直接使用 gem sources 查询结果如下: C:\Users\Administrator>gem ...

  7. 第二节 双向链表的GO语言实现

    一.什么是双向链表 和单链表比较,双向链表的元素不但知道自己的下线,还知道自己的上线(越来越像传销组织了).小煤车开起来,图里面可以看出,每个车厢除了一个指向后面车厢的箭头外,还有一个指向前面车厢的箭 ...

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

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

  9. MySql 使用explain分析查询

    今天写了个慢到哭的查询,想用explain分析下执行计划,后来发现explain也是有局限性的: EXPLAIN不会告诉你关于触发器.存储过程的信息或用户自定义函数对查询的影响情况 •EXPLAIN不 ...

  10. python generator

    def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('g ...