Since number could be 10^100, we have to use large number processing method, in string. A simpler method is to pre-calculate all Fibonacci numbers up to 101 digits, which is already fast enough.

//

#include <vector>
#include <iostream>
#include <string>
#include <cmath>
using namespace std; vector<string> dict; string plus_str(string &a, string &b)
{
unsigned sizeA = a.length();
unsigned sizeB = b.length();
unsigned sizeDlt = (unsigned)abs(float(sizeA - sizeB)); string &sL = sizeA > sizeB ? a : b;
string &sS = !(sizeA > sizeB) ? a : b; unsigned sizeL = max(sizeA, sizeB);
string ret(sizeL + , ''); int carry = ;
for(int i = sizeL - ; i >= ; i --)
{
int inxL = i;
int inxS = i - sizeDlt;
int inxR = i + ; int dL = sL[inxL] - '';
int dS = inxS < ? : sS[inxS] - ''; int sum = dL + dS + carry;
ret[inxR] = sum % +'';
carry = sum >= ? : ;
} if(carry)
{
ret[] = '';
}
else
{
ret.erase(, );
}
return ret;
} // 1 : a > b
// -1: a < b
// 0 : a == b
//
int comp(string &a, string &b)
{
unsigned sza = a.size();
unsigned szb = b.size();
if(sza != szb) return sza > szb ? : -; // the same length
for(unsigned i = ; i < sza; i ++)
{
int da = a[i] - '';
int db = b[i] - '';
if(da != db)
{
return da > db ? : -;
}
}
return ; // equal
} void fill_fib(vector<string> &dict)
{
dict.push_back("");
dict.push_back(""); bool bBreak = false;
int i = ;
while(!bBreak)
{
string newFib = plus_str(dict[i - ], dict[i - ]);
dict.push_back(newFib);
bBreak = newFib.length() >= ;
i ++;
}
} int get_fib_cnt(string &a, string &b)
{
int ret = ; unsigned nSize = dict.size();
for(int i = ; i <nSize; i ++)
{
if(comp(dict[i], b) == ) break;
if(comp(dict[i], a) >= )
{
cout << "Found " << dict[i] << endl;
ret ++;
}
} return ret;
} int main()
{ fill_fib(dict); string a, b;
cin >> a >> b; while(!(a == "" && b == ""))
{
cout << get_fib_cnt(a, b) << endl;
cin >> a >> b;
} return ;
}

I tried to find a closed form to Fibonacci only after I got a 0.01sec AC:
http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression

SPOJ #536. How many Fibs的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  5. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  6. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  7. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  8. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

  9. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

随机推荐

  1. C语言的位运算

    位运算加速技巧1. 如果乘上一个2的倍数数值,可以改用左移运算(Left Shift) 加速 300% x = x * 2;x = x * 64;//改为:x = x << 1; // 2 ...

  2. 将Excel生成实体类

    package com.excel.test; import java.util.List; public class createUtil { public static String append ...

  3. uva562 Dividing coins 01背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 救援行动(save) (BFS)

    时间限制: 1 Sec  内存限制: 64 MB提交: 42  解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...

  5. ipconfig /flushdns 解释

    当我们一域名的形式访问过目标网站后,该网站的域名和IP地址对应关系就会自动保存到本地工作站的DNS缓存列表中,如果以后再次访问该域名,浏览器就会先访问DNS缓存列表中的信息.但是,如果被访问网站的域名 ...

  6. JSBinding + SharpKit / 需要注意及不支持的列表

    1) 序列化不支持 public List<T>,其余都支持(JSBinding+Bridge无此功能) 2015年11月5日 补充:序列化只处理 Field.目前发现 Animation ...

  7. 11g RAC日志体系(cluster,database,asm,scan日志,ADRCI工具的使用)

  8. 特征值分解与奇异值分解(SVD)

    1.使用QR分解获取特征值和特征向量 将矩阵A进行QR分解,得到正规正交矩阵Q与上三角形矩阵R.由上可知Ak为相似矩阵,当k增加时,Ak收敛到上三角矩阵,特征值为对角项. 2.奇异值分解(SVD) 其 ...

  9. ROW_NUMBER、RANK、DENSE_RANK的用法

    --NND,索性把2005的一起帖出来. ROW_NUMBER.RANK.DENSE_RANK的用法 (爱新觉罗.毓华 -- 广东深圳) SQL Server 引入几个新的排序(排名)函数,如ROW_ ...

  10. hibernate--HQL语法与详细解释

    HQL查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hi ...