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. 用lucene4.10.2分词器进行分词

    import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.luc ...

  2. Core Java Volume I — 4.7. Packages

    4.7. PackagesJava allows you to group classes in a collection called a package. Packages are conveni ...

  3. (基础篇)PHP与JavaScript交互

    一.了解javascript Javascript是脚本编程语言,支持WEB应用程序的客户端和服务器端构件的开发,在WEB系统中得到了非常广泛的应用. 1. 什么是javascript Javascr ...

  4. iOS学习笔记---c语言第十一天

    函数指针 一.函数指针定义 //函数声明:声明我是一个什么函数 //求两个数的和 //函数的类型:int (int x,int y) //即:我是一个返回值为整型,有两个整型参数的函数. //函数名是 ...

  5. 227. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  6. Spring+Websocket实现消息的推送

    http://my.oschina.net/ldl123292/blog/304360

  7. MATLAB格式化输出控制 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    MATLAB格式化输出控制 format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long ...

  8. leetcode 109 Convert Sorted List to Binary Search Tree ----- java

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  9. 查看PHP的配置信息

    查看PHP的配置信息其实一个函数就搞定了. 首先在服务器的根目录建立phpinfo.php文件. 然后打开此文件输入以下内容 <?php phpinfo(); ?> 保存此文件之后在浏览器 ...

  10. java编程acm基础

    java还是不错的昂! import java.util.*; import java.io.*; public class text{ static int a=100; public static ...