Thứ Hai, 9 tháng 5, 2022

PMATRIX PASCAL

const   fin='PMATRIX.INP';
        fou='PMATRIX.OUT';
        maxN=500;
        oo=100000;
var a:array[1..maxN,1..maxN] of longint;
    b:array[1..2*oo] of boolean;
    p:array[0..2*oo] of longint;
    n,m:integer;
    lim,l:longint;
    min:longint;
    fi,fo:text;

procedure   sang(); //sang de tao mang p nguye ngto
    var i,j:longint;
    begin
    fillchar(b,sizeof(b),true);
    lim:=2*lim;
    b[1]:=false;
    for i:=2 to (lim div 2) do
        if (b[i]) then
            for j:=2 to (lim div i) do
                b[i*j]:=false;
    l:=0;
    p[0]:=0;
    for i:=1 to lim do
        if (b[i]) then
            begin
            inc(l);
            p[l]:=i;
            end;
    end;

function search(x:longint):longint;//tim so ngyen
    var left,right,mid:longint;//to nho nhat >= x
    begin
    left:=1;
    right:=l;
    repeat
        mid:=(left+right) div 2;
        if (p[mid]>=x) and (p[mid-1]<x) then
            exit(p[mid]);
        if (p[mid]>=x) then
            right:=mid-1;
        if (p[mid]<x) then
            left:=mid+1;
    until   left>right;
    end;

procedure nhap;
var i,j:longint;
begin
read(n,m);
for i:=1 to n do
           for j:=1 to m do 
    begin
              read(a[i,j]);
              if (a[i,j]>lim) then lim:=a[i,j];
    end;
end;

procedure   process();
var i,j:integer;
   s:longint;
begin
sang();
//cap nhat lai ma tran a[,]
//a[i,j] gio la so lan tang it nhat de a[i,j]
//ban dau la nguyen to
for i:=1 to n do
           for j:=1 to m do
              a[i,j]:=search(a[i,j])-a[i,j];

//tim so lan tang it nhat de 1 hang
//la nguyen to
min:=oo;
for i:=1 to n do begin
       s:=0;
   for j:=1 to m do s:=s+a[i,j];
   if (s<min) then min:=s;
   end;

//tim so lan tang it nhat de 1 cot la
//ngto
for j:=1 to m do
        begin
        s:=0;
        for i:=1 to n do
            s:=s+a[i,j];
        if (s<min) then min:=s;
        end;
write(min);
end;

begin
    assign(input,fin);reset(input);
    assign(output,fou);rewrite(output);
    process();
    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.