A password (a string of length ) is considered secure if it does not contain two consecutive zeros. The number of such strings is equal to the Fibonacci number f_n, defined as follows:
f_1 = 2 (the strings , ),
f_2 = 3 (the strings , , ),
f_n = f_{n-1} + f_{n-2}, n > 3
For example, the first Fibonacci numbers are:
The total number of binary strings of length is 2^n. Therefore, the expected number of randomly generated strings required to obtain a secure one is equal to
This fraction should be reduced by dividing the numerator and the denominator by their greatest common divisor.
Example
For n = 1, the answer is 2^1 / f_1 = 2 / 2 = 1 / 1.
For n = 4, the answer is 2^4 / f_4 = 16 / 8 = 2 / 1.
Algorithm implementation
Declare an array to store the Fibonacci numbers.
long long f[61];The function gcd computes the greatest common divisor of two numbers.
long long gcd(long long a, long long b)
{
return (!b) ? a : gcd(b, a % b);
}The main part of the program. Read the input value .
scanf("%d", &n);Compute the Fibonacci numbers.
f[0] = 1; f[1] = 2;
for (i = 2; i <= n; i++)
f[i] = f[i - 1] + f[i - 2];The answer is given as a fraction:
Reduce this fraction by dividing the numerator and the denominator by their greatest common divisor.
num = (1LL << n);
den = f[n];
d = gcd(num, den);
num /= d;
den /= d;Print the answer.
printf("%lld/%lld\n", num, den);