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. Windows上的文件合并命令

    从Linux转到Windowns后,发现很多好用的shell命令都没有了,但实际情况是Windows一样有DOS时代的命令窗口,在CLI年代用DOS的人也要干活. 比如,今天想将几个单独的sql文件整 ...

  2. textarea元素在加上runat="server"后运行报错解决

    当出现这个报错的时候,在后台引用相应的命名空间,为 using System.Web.UI; using System.Web.UI.HtmlControls;using System.Web.UI. ...

  3. SQLITE 时间字段操作函数

    SQLite中的时间日期函数 这是我学习SQLite时做的笔记,参考并翻译了Chris Newman写的<SQLite>中的<Working with Dates and Times ...

  4. 带同时滚动小色条的banner轮播图jq

    <div class="baoliao tongcheng"> <p class="headline1">同城<font>活 ...

  5. 关于freemarker标签+Spring3.0 V层学习

    import标签 就是把其他的ftl页面引用进来 <#import "/common/ui.ftl" as ui> 使用时 <@ui.message/>,m ...

  6. php随笔杂记(一)

    1.在function updatepwd($postData=array())   如果参数是一个数组, 在使用时,如果给他赋值则只返回数组名$postData即可  ,如果里面已有值 ,这返回的可 ...

  7. spark共享变量

    boradcast例子代码: scala版本 spark共享变量之Accumulator 例子代码: scala版本

  8. 对于python,一切事物都是对象,对象基于类创建

    新建列表.新建string字符串 li1 = [1, 2, 3, 4] li2 = list([1, 2, 3]) s1 = "abc" s2 = str("abc&qu ...

  9. struts2标签整理

    在JSP2.1中#被用作了JSP EL(表达式语言)的特殊记好,所以对OGNL的使用可能导致问题,   一个简单的方法是禁用JSP2.1的EL特性,这需要修改web.xml文件:   Java代码   ...

  10. 持续集成之Jenkins(坏老头)

    点击关注哦↑↑↑↑↑↑↑↑↑ 持续集成 有关持续集成的简介,可参考我此前的文章:http://blog.csdn.net/benkaoya/article/details/44993583 Jenki ...