TheAlgorithms-Python
62 строки · 1.8 Кб
1"""
2Project Euler Problem 115: https://projecteuler.net/problem=115
3
4NOTE: This is a more difficult version of Problem 114
5(https://projecteuler.net/problem=114).
6
7A row measuring n units in length has red blocks
8with a minimum length of m units placed on it, such that any two red blocks
9(which are allowed to be different lengths) are separated by at least one black square.
10
11Let the fill-count function, F(m, n),
12represent the number of ways that a row can be filled.
13
14For example, F(3, 29) = 673135 and F(3, 30) = 1089155.
15
16That is, for m = 3, it can be seen that n = 30 is the smallest value
17for which the fill-count function first exceeds one million.
18
19In the same way, for m = 10, it can be verified that
20F(10, 56) = 880711 and F(10, 57) = 1148904, so n = 57 is the least value
21for which the fill-count function first exceeds one million.
22
23For m = 50, find the least value of n
24for which the fill-count function first exceeds one million.
25"""
26
27from itertools import count28
29
30def solution(min_block_length: int = 50) -> int:31"""32Returns for given minimum block length the least value of n
33for which the fill-count function first exceeds one million
34
35>>> solution(3)
3630
37
38>>> solution(10)
3957
40"""
41
42fill_count_functions = [1] * min_block_length43
44for n in count(min_block_length):45fill_count_functions.append(1)46
47for block_length in range(min_block_length, n + 1):48for block_start in range(n - block_length):49fill_count_functions[n] += fill_count_functions[50n - block_start - block_length - 151]52
53fill_count_functions[n] += 154
55if fill_count_functions[n] > 1_000_000:56break57
58return n59
60
61if __name__ == "__main__":62print(f"{solution() = }")63