Fibonacci numbers
The Fibonacci sequence is one of the most well-known sequences in mathematics and computer science. It is named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci.
The Fibonacci numbers f_n = f(n) form a sequence where each number after the first two is equal to the sum of the two preceding ones.
They are defined by the following recurrence relation:
Thus, we have:
The Fibonacci sequence therefore begins as:
Each term is obtained by adding the two previous terms.
Example. Compute Fibonacci numbers using an array.
The following program fills an integer array such that fib[i] = f_i, where f_i is the -th Fibonacci number. It then prints the value for a given input .
#include <stdio.h>
int i, n, fib[47];
int main(void)
{
scanf("%d",&n);
fib[0] = 0; fib[1] = 1;
for(i = 2; i <= n; i++)
fib[i] = fib[i-1] + fib[i-2];
printf("%d\n",fib[n]);
return 0;
}The largest Fibonacci number that can be stored in the int data type is:
The largest Fibonacci number that can be stored in the long long data type is:
For computing Fibonacci numbers with index n > 92, standard integer types are no longer sufficient due to overflow. In such cases, arbitrary-precision arithmetic must be used, such as the BigInteger type (or equivalent big number libraries in programming languages that do not support built-in large integers).
Example. Compute the -th Fibonacci number using a recursive function.
The following program implements a direct recursive definition of the Fibonacci sequence. The function returns the -th Fibonacci number according to the recurrence relation.
#include <stdio.h>
int n;
int fib(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n - 2);
}
int main(void)
{
scanf("%d",&n);
printf("%d\n",fib(n));
return 0;
}Example. Compute the -th Fibonacci number using recursion with memoization.
The following program implements a recursive solution enhanced with memoization to avoid redundant computations. Previously computed values are stored in an array, so each Fibonacci number is calculated at most once.
#include <stdio.h>
#include <string.h>
int n, fib[46];
int f(int n)
{
// base cases
if (n == 0) return 0;
if (n == 1) return 1;
// if fib[n] has already been computed, return it
if (fib[n] != -1) return fib[n];
// otherwise compute and store (memoize) the result
return fib[n] = f(n-1) + f(n - 2);
}
int main(void)
{
scanf("%d",&n);
// initialize memoization array:
// fib[i] = -1 indicates that the value has not been computed yet
memset(fib,-1,sizeof(fib));
printf("%d\n",f(n));
return 0;
}This approach significantly improves efficiency compared to plain recursion. While the naive recursive solution has exponential complexity, memoization reduces the time complexity to , since each state is evaluated only once.
Java code
import java.util.*;
public class Main
{
static int fib[] = new int[46];
static int f(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
if (fib[n] != -1) return fib[n];
return fib[n] = f(n-1) + f(n - 2);
}
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
Arrays.fill(fib, -1);
System.out.println(f(n));
con.close();
}
}Prove the following properties of Fibonacci numbers:
► Base case n = 0: f_0 = f_2 - 1. This is true since 0 = 1 - 1.
Inductive step. Assume that:
Then:
► Base case n = 1: f_1 = f_2, which holds since .
Inductive step. Assume:
Then:
► Base case n = 1: f_2 = f_3 - 1. This is true since 1 = 2 - 1.
Inductive step. Assume:
Then:
► Base case n = 0: f_0^2 = f_0 \cdot f_1. This is true since 0 = 0 \cdot 1.
Inductive step. Assume:
Then:
NO two one's in a row
Find the number of sequences of length , consisting only of zeros and ones, that do not have two one's in a row.
Let be the number of sequences consisting of and of length that do not have two one's in a row.
If the first number in the sequence is , then starting from the second place we can build f(n - 1) sequences.
If the first number in the sequence is , then second number must be . In this case, starting from the third position, we can build f(n - 2) sequences.
We have Fibonacci numbers with base cases f(1) = 2, f(2) = 3.
Compute the number of sequences of length , consisting only of zeros and ones, where there are no three consecutive ones.
Input. One integer n~(1 \le n \le 10^5) is given — the length of the sequence.
Output. Print the number of such sequences modulo .
12413As is well known, the Fibonacci sequence is defined as follows:
It is named after the Italian mathematician Leonardo Fibonacci, also known as Leonardo of Pisa.
Given two integers and , find the greatest common divisor of F_n and F_m.
Input. Each line represents a single test case and contains two integers and m~(1 \le n, m \le 10^{18}). The number of test cases does not exceed .
Output. For each test case, print on a separate line the value of GCD(F_n, F_m) modulo 10^8.
2 3
1 1
100 2001
1
61915075As is well known, the Fibonacci numbers are defined as follows:
Given two integers and , compute the sum:
Input. Each line represents a separate test case and contains two integers and b~(0 \le a \le b \le 10^9).
Output. For each test case, print on a separate line the value of modulo 10^9 + 7.
1 1
3 5
10 10001
10
625271457Generate the -th Fibonacci string, which is defined by the following recurrence relation:
f(0) = "a";
f(1) = "b";
f(n) = f(n - 1) + f(n - 2), where the "" denotes string concatenation.
For example:
Input. One integer n~(0 \le n \le 20).
Output. Print the -th Fibonacci string.
3bab5babbababWhile the students are taking an exam, the teachers are playing Mafia. There are teachers sitting around a round table. The host must deal ace cards to some of them (the number of aces can be arbitrary, including ) — these teachers will be the mafia. However, no two mafia members are allowed to sit next to each other.
In how many ways can the host deal the cards? Two ways are considered different if there exists at least one teacher who is a mafia member in one case and is not a mafia member in the other.
Input. The number of teachers n~(1 \le n \le 30) sitting around the table.
Output. Print one integer — the number of ways to deal the cards.
1223All containers in the world fall into two categories — with TNT and without.
Only a fool would place a TNT box on top of another TNT box. Since you're clearly not one of them (right?), you know very well that TNT explodes, especially if another TNT box is placed on top of it.
You find yourself in a room filled with a vast number of boxes of both types. Suddenly, a lift emerges from a hatch in the floor. Unfortunately, it is malfunctioning. It has decided to build a tower of boxes. To assess your chances of survival, you need to calculate the number of possible configurations in which nothing explodes.
By the way, think about it: what is a rational person like you doing in a room full of TNT?

Input. One integer n~(1 \le n < 45).
Output. Print the number of safe ways to build the tower.
1223A bee, moving inside a honeycomb, can move as shown in the figure:
by moves and — from the upper row,
by move — from the lower row.

Input. The number of hexagons n~(1 \le n \le 45) in the upper row is given. The lower row contains one hexagon fewer.
Output. Print the number of ways in which the bee can reach the last cell of the upper row starting from the first cell of the same row.
1132Jomart uses a binary string as the password for his computer. Recently, he forgot his old password and now wants to obtain a new one, which will be a binary string of length . He considers a password sufficiently secure if it does not contain two consecutive zeros.
To obtain a new password, Jomart generates a random binary string of length . If the string is not secure, he generates another one and repeats the process until he obtains a secure password.
Find the expected number of randomly generated passwords Jomart will need before he finds a secure one.
Input. One integer n~(1 \le n \le 60).
Output. Print the expected value as a fraction , where and are coprime positive integers.
11/142/1The flag consists of vertical stripes, each of which can be colored white, red, or blue. Moreover:
No two adjacent stripes may have the same color.
Any blue stripe must be placed between a red and a white stripe (in any order).
How many ways are there to color a flag with stripes?
Input. One integer n~(1 \le n \le 10^6) — the number of stripes on the flag.
Output. Print the number of ways to color a flag with stripes. The answer should be given modulo 10^9 + 7.
34Find the number of ways to completely tile a rectangle of size 2 \times n with dominoes of size 2 \times 1. Coverings that coincide with themselves under symmetries (rotations or reflections) are considered different.
Input. One integer n~(0 < n < 65536).
Output. Print the number of ways to tile the rectangle with dominoes.
1145The Fibonacci numbers are defined as follows:
Compute the -th Fibonacci number.
Input. The first line contains the number of test cases t~(1 \le t \le 10^3). Each of the next lines contains one integer n~(1 \le n \le 10^4).
Output. For each test case, print the corresponding Fibonacci number on a separate line.
5
1
2
3
4
51
1
2
3
5For the given integers a, b, c, n, p, find the value of the expression:
Print the result as two numbers and such that
Input. Five integers a, b, c, n, p are given. It is known that:
p < 10^9 is a prime number,
0 \le a, b < p,
1 \le c < p,
0 \le n \le 10^{18}
Output. Print two integers and — the coefficients of and \sqrt{c} respectively, modulo .
Note. You should work in the extended field:
Operation rules:
Addition
(x_1 + y_1 \sqrt{c}) + (x_2 + y_2 \sqrt{c}) = (x_1 + x_2) + (y_1 + y_2) \sqrt{c}Multiplication
(x_1 + y_1 \sqrt{c}) \cdot (x_2 + y_2 \sqrt{c}) = (x_1 x_2 + c y_1 y_2) + (x_1 y_2 + x_2 y_1) \sqrt{c}
All operations are performed modulo .
Examples. In the first example:
In the second example:
2 1 5 2 179 43 2 5 10 171 2For the given integers a, b, c, n, p, compute the value of the sum:
Print the result as two numbers and such that
Input. Five integers a, b, c, n, p are given. It is known that:
p < 10^9 is a prime number,
0 \le a, b < p,
1 \le c < p,
0 \le n \le 10^9
Output. Print two integers and — the coefficients of and \sqrt{c} respectively, modulo .
Note. You should work in the extended field:
Operation rules:
Addition
(x_1 + y_1 \sqrt{c}) + (x_2 + y_2 \sqrt{c}) = (x_1 + x_2) + (y_1 + y_2) \sqrt{c}Multiplication
(x_1 + y_1 \sqrt{c}) \cdot (x_2 + y_2 \sqrt{c}) = (x_1 x_2 + c y_1 y_2) + (x_1 y_2 + x_2 y_1) \sqrt{c}
All operations are performed modulo .
Examples. In the first example:
In the second example:
2 1 5 2 179 43 2 5 10 171 2Given two integers and , compute the value:
where f_i is the -th Fibonacci number.
The Fibonacci sequence is defined as follows:
Since the answer can be very large, print it modulo 10^9 + 7.
Input. One line contains two integers and k~(1 \le n \le 10^{18}, 1 \le k \le 10^5) where:
is the number of terms in the sum,
is the exponent applied to each Fibonacci number.
Output. Print one integer — the value of the sum:
4 175 109825700

Comments2