program pazzle;
{$mode objfpc}{$B-}
uses
SysUtils;
type
TDigit = 0..9;
procedure Solve;
var
State: set of TDigit;
E, I, K, L, M, N, R, S, T, Count: TDigit;
EN, ET, ST, RS, TERS, ENEN, METTIEN, ERST, KENEN, TTERS, KERST: Integer;
REKENLES1, REKENLES2: Int64;
function TrueMod10: Boolean;
begin
Result := (10 - Integer(S) + Integer(N) * Succ(Integer(T))) mod 10 = T;
end;
function TrueMod100: Boolean;
begin
RS := Integer(R) * 10 + S;
EN := Integer(E) * 10 + N;
ST := Integer(S) * 10 + T;
ET := Integer(E) * 10 + T;
Result := (100 - RS + EN + EN * ET) mod 100 = ST;
end;
function TrueMod10000: Boolean;
begin
TERS := Integer(T) * 1000 + Integer(E) * 100 + RS;
ENEN := EN * 100 + EN;
METTIEN := (Integer(M) * 100 + ET) * (Integer(T) * 1000 + Integer(I) * 100 + EN);
ERST := Integer(E) * 1000 + Integer(R) * 100 + ST;
Result := (10000 - TERS + ENEN + METTIEN) mod 10000 = ERST;
end;
function TrueMod100000: Boolean;
begin
TTERS := T * 10000 + TERS;
KENEN := Integer(K) * 10000 + ENEN;
KERST := Integer(K) * 10000 + ERST;
Result := (100000 - TTERS + KENEN + METTIEN) mod 100000 = KERST;
end;
function IsSolution: Boolean;
begin
Result := R * 1000000 + E * 100000 + KENEN + METTIEN - (L * 1000000 + E * 100000 + TTERS) = KERST;
end;
function CalcREKENLES: Int64;
begin
Result := Int64(S) + Int64(E) * 10 + Int64(L) * 100 + Int64(N) * 1000 + Int64(E) * 10000 +
Int64(K) * 100000 + Int64(E) * 1000000 + Int64(R) * 10000000;
end;
begin
State := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Count := 0;
REKENLES1 := 0;
REKENLES2 := 0;
for N := 0 to 9 do
begin
Exclude(State, N);
for S := 0 to 9 do
if S in State then
begin
Exclude(State, S);
for T := 0 to 9 do
if (T in State) and TrueMod10 then
begin
Exclude(State, T);
for E := 0 to 9 do
if E in State then
begin
Exclude(State, E);
for R := 0 to 9 do
if (R in State) and TrueMod100 then
begin
Exclude(State, R);
for I := 0 to 9 do
if I in State then
begin
Exclude(State, I);
for M := 0 to 9 do
if (M in State) and TrueMod10000 then
begin
Exclude(State, M);
for K := 0 to 9 do
if (K in State) and TrueMod100000 then
begin
Exclude(State, K);
for L := 0 to 9 do
if (L in State) and IsSolution then
begin
Inc(Count);
Write('K=',K);
Write(', E=',E);
Write(', R=',R);
Write(', S=',S);
Write(', T=',T);
Write(', N=',N);
Write(', M=',M);
Write(', I=',I);
Write(', L=',L);
Write(' MINSTREEL=',M,I,N,S,T,R,E,E,L,', ');
WriteLn('REKENLES=',R,E,K,E,N,L,E,S);
if Count = 1 then
REKENLES1 := CalcREKENLES;
if Count = 2 then
begin
REKENLES2 := CalcREKENLES;
WriteLn(REKENLES1,' * ', REKENLES2,' = ',
REKENLES1 * REKENLES2);
exit;
end;
end;
Include(State, K);
end;
Include(State, M);
end;
Include(State, I);
end;
Include(State, R);
end;
Include(State, E);
end;
Include(State, T);
end;
Include(State, S);
end;
Include(State, N);
end;
end;
var
Ticks: QWord;
begin
Ticks := GetTickCount64;
Solve;
Ticks := GetTickCount64 - Ticks;
WriteLn('tick count = ', Ticks);
ReadLn;
end.