## Despite the popularity of Hilbert’s infinite hotel, Hilbert decided to try managing extremely large finite hotels, instead.

To cut costs, Hilbert wished to power the new hotel with his own special generator. Each floor would send power to the floor above it, with the top floor sending power back down to the bottom floor. That way, Hilbert could have the generator placed on any given floor (as he likes having the option) and have electricity flow freely throughout the entire hotel.

Unfortunately, the contractors misinterpreted the schematics when they built the hotel. They informed Hilbert that each floor sends power to another floor at random, instead. This may compromise Hilbert’s freedom to have the generator placed anywhere, since blackouts could occur on certain floors.

For example, consider a sample flow diagram for a three-story hotel:

If the generator were placed on the first floor, then every floor would receive power. But if it were placed on the second or third floors instead, then there would be a blackout on the first floor. Note that while a given floor can receive power from many other floors at once, it can only send power to one other floor.

To resolve the blackout concerns, Hilbert decided to have a minimal number of floors rewired. To rewire a floor is to change the floor it sends power to. In the sample diagram above, all possible blackouts can be avoided by rewiring the second floor to send power to the first floor instead of the third floor.

Let $F(n)$ be the sum of the minimum number of floor rewirings needed over all possible power-flow arrangements in a hotel of $n$ floors. For example, $F(3) = 6$, $F(8) = 16276736$, and $F(100) \bmod 135707531 = 84326147$.

Find $F(12344321) \bmod 135707531$.

### To solve this problem, we first need to determine the number of ways to represent an integer `n` as the sum of different Fibonacci numbers. Let’s write a Python function to calculate `f(n)`.

“`python

def f(n):

fibonacci = [1, 2]

ways = [1] + [0] * n

for num in fibonacci:

for i in range(num, n+1):

ways[i] += ways[i – num]

return ways[n]

“`

The `fibonacci` list contains the Fibonacci numbers, starting with 1 and 2. The `ways` list is initialized to have `0` in all positions except the first position, which is set to `1` to handle the base case `f(0) = 1`.

We then iterate over each Fibonacci number in the `fibonacci` list and update the `ways` list. For each number `num`, we update `ways[i]` by adding the number of ways to represent `i – num` using the previous Fibonacci numbers.

Next, we need to calculate `S(n)` up to `n = 10^13`. To do this, we can utilize the fact that `S(n)` is the sum of all `f(k)` for `k` ranging from `0` to `n`. Since `f(n)` can be calculated efficiently, we can avoid recalculating the same values multiple times.

“`python

def S(n):

fibonacci_sum = [0] * (n+1)

for i in range(n+1):

fibonacci_sum[i] = fibonacci_sum[i-1] + f(i)

return fibonacci_sum[n]

“`

The `fibonacci_sum` list is initialized to have `0` in all positions. We then iterate over each number `i` from `0` to `n` and calculate the cumulative sum of `f(k)` from `0` to `i`. This is stored in the `fibonacci_sum` list. Finally, we return `fibonacci_sum[n]` which represents `S(n)`.

Now, let’s calculate `S(10^13)` using the provided functions.

“`python

result = S(10**13)

print(result)

“`

This will output the value of `S(10^13)`. However, note that calculating `S(10^13)` will take a significant amount of time and computational resources. Consider using a more efficient algorithm or finding a different approach to solve the problem if the solution takes too long.

##### More Answers:

Tricoloured Coin FountainsSimbers

Smallest Prime Factor