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. HTML-day-2-HTML常用标签

    一.常用标签 超链接标签 href-----hyperlink reference _blank,是指在新窗口中打开. 作用:①做锚点的标签,<a name=””></a> ② ...

  2. 一步一步使用sklearn

    http://kukuruku.co/hub/python/introduction-to-machine-learning-with-python-andscikit-learn Hello, %u ...

  3. nagios安装配置

     http://www.codeweblog.com/nagios%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/ 上线的服务器有时会被人攻击,导致服务不可用,今天安装配置了 ...

  4. Alice and Bob

    类似于石子合并的游戏,在黑板上写下N个数,每次只能将其中的一个数减1(结果为0自动消去),或者将某两个数消去,将其和写在黑板上. Alice先手,彼此都采用最优策略,将最后一个数消去者获胜. 思路:设 ...

  5. uva562 Dividing coins 01背包

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

  6. Websphere发布时遇到的问题

    在发布时遇到了data source配置的问题,搞了好久没搞定,最后问题出现在 JDBC providers那边,Implementation class name 改成: com.ibm.db2.j ...

  7. 02java语法基础问题总结

    S和t引用的不是同一个对象 不是原始数据类型 结论: 枚举类型是引用类型,枚举不属于原始数据类型.它的每一个具体值都引用一个特定的对象. 2. 以下代码的输出结果是什么? int X=100; int ...

  8. Oracle 删除用户和表空间

    版权声明:本文为博主原创文章,未经博主允许不得转载. Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用 ...

  9. 工作中遇到的问题--实现CustomerSetting的实时更新

    首先在项目运行时就初始化CustomerSettings的值,采用@Bean,默认是singtone模式,只会加载一次. @Configuration@Order(3)@EnableWebMvcSec ...

  10. 关于setInterval()里的this和细节

    setInterval(fn,t);里的fn中,要使用外部类的this,则需要先将this保存起来,再使用保存的this,不能直接使用this,里面的this是指向window对象,记住setInte ...