Thứ Tư, 27 tháng 4, 2022

QBSEQ pascal

const fin = 'qbseq.inp';   fon = 'qbseq.out';
   maxk = 100;maxn=1000;vc=100;

var
   a:array[1..maxn] of longint;
   f:array[0..maxn,0..maxk] of longint;
   n,s,t,k:longint;
procedure enter;
var i:longint;
begin
    read(n,k); s:=0;
    for i:=1 to n do
    begin
        read(a[i]);
        s:=s+a[i];
    end;
    s:=s mod k;
end;

function modk(x,y:longint):longint;
begin
    t:=(x-y) mod k;
    if t>0 then exit(t)
    else exit(t+k);
{if x>y then exit((x-y) mod k)
else exit((x+k-y) mod k);}
end;

procedure solve;
var i,j,sum:longint;
begin
    f[0,0]:=0;
    for i:=1 to k-1 do f[0,i]:=vc;
    for i:=1 to n do
        for j:=0 to k-1 do
        begin
            f[i,j]:=f[i-1,modk(j,a[i])]+1;
            if f[i-1,j]<f[i,j] then f[i,j]:=f[i-1,j];
        end;
    writeln(n - f[n,s]);

end;

procedure truyvet;
var sum:longint;
begin
    {truy vet}
    sum:=0;
    repeat
        if f[n,s]=f[n-1,s] then
        begin
            writeln('a[',n,']=',' ',a[n]);
            sum:=sum+a[n];
        end
        else s:=modk(s,a[n]);
        dec(n);
    until n=0;
    write(sum);
end;

begin
    assign(input,fin);reset(input);
    assign(output,fon);rewrite(output);
    enter;
    solve;
  //  truyvet;
    close(input);close(output);
end.

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.