Flags#9558

Flags (Editorial)

Let:

Let’s consider how to compute the function f_r(n):

Thus, we obtain the following equality:

f_r(n) = f_w(n - 1) + f_w(n - 2)

By analogous reasoning, we obtain:

f_w(n) = f_r(n - 1) + f_r(n - 2)

The initial conditions for the first recurrence are obvious:

Similarly,

f_w(1) = 1, f_w(2) = 1

Both functions f_r(n) and f_w(n) define Fibonacci numbers:

f_r(n) = F_n, \\ f_w(n) = F_n

The coloring of the flag starts with the first stripe. It can be either red or white. Therefore, the total number of ways to color the flag is

f_r(n) + f_w(n) = 2 * F_n

Algorithm implementation

Declare the constants.

#define MAX 1000001
#define MOD 1000000007

Declare an array to store the Fibonacci numbers.

long long fib[MAX];

Read the input value .

scanf("%d", &n);

Fill the array with Fibonacci numbers.

fib[1] = 1; fib[2] = 1;
for (i = 3; i < MAX; i++) 
  fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;

Print the answer.

printf("%lld\n", (2 * fib[n]) % MOD);