Fibonacci sequence

Posted by rob.weaver on Mon, 20 Dec 2021 10:01:54 +0100

1, Recursion

It is known that the Fibonacci sequence Fn = Fn − 1 + Fn − 2 (n > = 3), F1 = 1,F2 = 1. The nth term of the sequence is solved recursively.

Input format:

Enter a positive integer n (1 < = n < = 40).

Output format:

Output a number, the nth item of the sequence

Input example 1:

1

No blank lines at the end

Output example 1:

1

No blank lines at the end

Input example 2:

3

No blank lines at the end

Output example 2:

2

No blank lines at the end

Answer:

#include <bits/stdc++.h>
using namespace std;
int myfunction(int n)
{
    if (n <= 2)
        return 1;
    else
        return myfunction(n - 1) + myfunction(n - 2);
}
int main()
{
    int n;
    cin >> n;
    cout << myfunction(n);
    return 0;
}

Because the range of the input positive integer is 1 ~ 40, the time and space occupied by recursion are within the range.

2, Circulation

The Fibonacci sequence is known as Fn = Fn − 1 + Fn − 2 (n > = 3), F1 = 1,F2 = 1

The nth term of the sequence is solved, and the result is the modulus of 998244353.

Input format:

Enter a positive integer n (1 < = n < = 10000000).

Output format:

Output a number, the nth item of the sequence

Input example 1:

1

No blank lines at the end

Output example 1:

1

No blank lines at the end

Input example 2:

3

No blank lines at the end

Output example 2:

2

No blank lines at the end

Answer:

#include <bits/stdc++.h>
using namespace std;
int myfunction(int n)
{
    if (n <= 2)
        return 1;
    else
    {
        int x = 1, y = 1, z, i;
        for (i = 3; i <= n; i++)
        {
            z = (x + y) % 998244353;
            x = y;
            y = z;
        }
        return z;
    }
}
int main()
{
    int n;
    cin >> n;
    cout << myfunction(n);
    return 0;
}

Recursive algorithms can be expressed by circular statements. When the problem scale is too large, the memory space occupied by recursion will be more and more (constantly applying for local variables in new recursive functions), which may eventually lead to insufficient space and calculation errors. Therefore, when the problem scale is too large, we'd better use circular statements to solve the problem.

3, Matrix fast power

The Fibonacci sequence is known as Fn = Fn − 1 + Fn − 2 (n > = 3), F1 = 1,F2 = 1

The nth term of the sequence is solved, and the result is the modulus of 998244353.

Prompt: fast power of matrix, maximum value of unsigned long: 1844674407370955161 (1.8e18)

Input format:

Enter a positive integer n (1 < = n < = 1e18).

Output format:

Output a number, the nth item of the sequence

Input example 1:

1

No blank lines at the end

Output example 1:

1

No blank lines at the end

Input example 2:

3

No blank lines at the end

Output example 2:

2

No blank lines at the end

Answer:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define m 998244353
typedef struct matrix
{
    ull node[2][2];
} ma;
ma chengfa(ma a, ma b)
{
    ma c;
    int i, j, k;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
        {
            c.node[i][j] = 0;
            for (k = 0; k < 2; k++)
            {
                c.node[i][j] += a.node[i][k] * b.node[k][j];
                c.node[i][j] %= m;
            }
        }
    return c;
}
ma jzksm(ull n, ma x)
{
    ma y;
    y.node[0][0] = 1;
    y.node[0][1] = 0;
    y.node[1][0] = 0;
    y.node[1][1] = 1;
    while (n)
    {
        if (n & 1)
            y = chengfa(y, x);
        x = chengfa(x, x);
        n >>= 1;
    }
    return y;
}
int main()
{
    ull n;
    cin >> n;
    if (n < 3)
        cout << 1;
    else
    {
        ma x;
        x.node[0][0] = 1;
        x.node[0][1] = 1;
        x.node[1][0] = 1;
        x.node[1][1] = 0;
        x = jzksm(n - 2, x);
        cout << (x.node[0][0] + x.node[0][1]) % m;
    }
    return 0;
}

When the problem scale becomes larger again, if you still use a simple for loop to solve the problem, it will take a long time. The fast power of matrix is derived from the fast power of constant.

Topics: C Algorithm