106 lines
2.5 KiB
Plaintext
106 lines
2.5 KiB
Plaintext
{
|
|
Solutions to the Advent Of Code.
|
|
Copyright (C) 2024 Stefan Müller
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
}
|
|
|
|
unit UBinomialCoefficients;
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, Generics.Collections;
|
|
|
|
type
|
|
TCardinalArray = array of Cardinal;
|
|
TCardinalArrays = specialize TList<TCardinalArray>;
|
|
|
|
{ TBinomialCoefficientCache }
|
|
|
|
TBinomialCoefficientCache = class
|
|
private
|
|
FCache: TCardinalArrays;
|
|
procedure AddRow;
|
|
public
|
|
constructor Create;
|
|
destructor Destroy; override;
|
|
// Returns N choose K, with N >= K >= 0.
|
|
function Get(const AN, AK: Cardinal): Cardinal;
|
|
// Returns the number of cached rows C = N + 1, where N is the highest from previously queried "N choose K". The
|
|
// actual number of cached binomial coefficient values is C * (C + 1) / 2.
|
|
function GetCachedRowsCount: Cardinal;
|
|
end;
|
|
|
|
var
|
|
BinomialCoefficients: TBinomialCoefficientCache;
|
|
|
|
implementation
|
|
|
|
{ TBinomialCoefficientCache }
|
|
|
|
procedure TBinomialCoefficientCache.AddRow;
|
|
var
|
|
row: TCardinalArray;
|
|
i: Cardinal;
|
|
begin
|
|
SetLength(row, FCache.Count + 1);
|
|
row[0] := 1;
|
|
if FCache.Count > 0 then
|
|
begin
|
|
row[FCache.Count] := 1;
|
|
for i := 1 to FCache.Count - 1 do
|
|
row[i] := FCache.Last[i - 1] + FCache.Last[i];
|
|
end;
|
|
FCache.Add(row);
|
|
end;
|
|
|
|
constructor TBinomialCoefficientCache.Create;
|
|
begin
|
|
FCache := TCardinalArrays.Create;
|
|
end;
|
|
|
|
destructor TBinomialCoefficientCache.Destroy;
|
|
begin
|
|
FCache.Free;
|
|
inherited Destroy;
|
|
end;
|
|
|
|
function TBinomialCoefficientCache.Get(const AN, AK: Cardinal): Cardinal;
|
|
var
|
|
i: Cardinal;
|
|
begin
|
|
if AN < AK then
|
|
raise ERangeError.Create('Cannot calculate binomial coefficient "n choose k" with k larger than n.');
|
|
|
|
for i := FCache.Count to AN do
|
|
AddRow;
|
|
Result := FCache[AN][AK];
|
|
end;
|
|
|
|
function TBinomialCoefficientCache.GetCachedRowsCount: Cardinal;
|
|
begin
|
|
Result := FCache.Count;
|
|
end;
|
|
|
|
initialization
|
|
BinomialCoefficients := TBinomialCoefficientCache.Create;
|
|
|
|
finalization
|
|
BinomialCoefficients.Free;
|
|
|
|
end.
|
|
|