FBRICK - Xếp hình

Tác giả: ladpro98

Ngôn ngữ: Pascal

program FBRICK;
uses    math;
const   fi = '';
        fo = '';
type    arr = array[1..6,1..6] of int64;
var     s, c: arr;
        Q:array[1..6] of int64;
        n, m , k, res, kk: int64;
        t, tt, i: longint;
        inp, oup:text;

operator *(a,b: arr) c: arr;
var     i,j,k:longint;
begin
        for i:=1 to 6 do
        for j:=1 to 6 do begin
                C[i,j] := 0;
                for k:=1 to 6 do begin
                        C[i,j] := (C[i,j] + a[i,k]*b[k,j]) mod M;
                end;
                if c[i,j] < 0 then inc(c[i,j], m);
        end;
end;

function Pow(p: longint):  arr;
var     t: arr;
begin
        if p = 1 then exit(S);
        t := Pow(p shr 1);
        t := t * t;
        if odd(p) then exit(t * S);
        exit(t);
end;

begin
        assign(inp,fi);reset(inp);
        assign(oup,fo);rewrite(oup);
        readln(inp,t);
        for tt:=1 to t do begin
                readln(inp,kk,n,m);
                k := 2*kk mod m;
                Q[1] := 1; Q[2] := kk*kk mod m; Q[3] := 1;
                Q[4] := kk; Q[5] := kk; Q[6] := 1;
                S[1,1] := 1; S[1,2] := 1; S[2,2] := k*k mod m;
                S[2,3] := 1; S[2,4] := -2*k; S[3,2] := 1;
                S[4,2] := k; S[4,4] := -1; S[5,5] := k;
                S[5,6] := -1; S[6,5] := 1;
                C := Pow(n-1);
                res := 0;
                for i:=1 to 6 do begin
                        res := (res + Q[i] * C[1,i]) mod M;
                        //if res < 0 then inc(res, m);
                end;
                writeln(oup,res);
        end;
        close(oup);
end.

Download