斐波那契数列的一种计算方法

来源:互联网 发布:jre for windows.exe 编辑:程序博客网 时间:2024/06/14 10:43
#include <cstdio>#include <map>using namespace std;map<int, int> m;const int md = 19999997;//需要模的数long long f(const int x) {    if(x <= 2) return x ? 1 : 0;    if(m.count(x)) return m[x];    const int k = x >> 1;    const long long a = f(k);    const long long b = f(k + 1);    if(x & 1) return m[x] = (a * a + b * b) % md;    else return m[x] = a * (b + b - a + md) % md;}int main() {    int n;    scanf("%d", &n);    printf("%d\n", int(f(n + 1)));    return 0;}
0 0