program Hats;
  const k=3;
  var N,i,A,B,C,D:longint;
      j,r,x,Count:integer;
      H:array[1..k] of integer;
begin
  randomize;
  write('Число опытов N=');readln(N);
  A:=0; B:=0; C:=0; D:=0;
  for i:=1 to N do
  begin
    for j:=1 to k do H[j]:=j;
    for j:=k downto 1 do
    begin
      r:=random(j)+1;
      x:=H[j];H[j]:=H[r];H[r]:=x;
    end;
    Count:=0;
    for j:=1 to k do if H[j]=j then inc(Count);
    case Count of
      3:inc(A);
      0:inc(B);
      1:inc(C);
      2:inc(D);
    end;
  end;
  writeln('P(A)=',A/N:7:5);
  writeln('P(B)=',B/N:7:5);
  writeln('P(C)=',C/N:7:5);
  writeln('P(D)=',D/N:7:5);
  writeln('Нажмите клавишу Enter ...');
  readln;
end.
