Project Euler 150
Project Euler 150
题目
Searching a triangular array for a sub-triangle having minimum-sum
In a triangular array of positive and negative integers, we wish to find a sub-triangle such that the sum of the numbers it contains is the smallest possible.
In the example below, it can be easily verified that the marked
triangle satisfies this condition having a sum of
We wish to make such a triangular array with one thousand rows, so we generate 500500 pseudo-random numbers s_k in the range ±2^19, using a type of random number generator (known as a Linear Congruential Generator) as follows:
Thus:
Our triangular array is then formed using the pseudo-random numbers thus:
Sub-triangles can start at any element of the array and extend down as far as we like (taking-in the two elements directly below it from the next row, the three elements directly below from the row after that, and so on).
The “sum of a sub-triangle” is defined as the sum of all the elements it contains.
Find the smallest possible sub-triangle sum.
解决方案
将数字三角形直接转化成一个以左下角为顶点的直角三角形,方便存储。如下:
那么令三角形的行数为
假设
那么,对于最上面的点
可以看出,当
由上式可以提供一种做法:先枚举最上面的点
这种做法需要枚举
代码
1 | # include <bits/stdc++.h> |