about summary refs log tree commit diff
path: root/12/TP-2008/R2/TBC.PAS
blob: 1a26a91f0bdf6d5f16b0fc1c6a43c695a9413168 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var
  f : text;
  i, l, m, n : integer;
  a : array[1..1000] of longint;

procedure qsort(l, h : integer);
  var
    i, j : integer;
    x, tmp : longint;
  begin
    i := l;
    j := h;
    x := a[(i + j) div 2];
    repeat
      while a[i] < x do inc(i);
      while x < a[j] do dec(j);
      if i <= j then
        begin
          tmp := a[i];
          a[i] := a[j];
          a[j] := tmp;
          inc(i);
          dec(j)
        end
    until i > j;
    if l < j then qsort(l, j);
    if i < h then qsort(i, h)
  end;

function biin(n0 : integer) : boolean;
  var l, h, m : integer;
  begin
    l := 1;
    h := n;
    repeat
      m := (l + h) div 2;
      if a[m] = n0 then exit(true);
      if a[m] < n0 then l := m + 1
      else h := m - 1
    until l > h;
    biin := false
  end;

function libtbc(n0 : integer) : boolean;
  var i, j : integer;
  begin
    for i := 1 to n0 - 1 do
      for j := n0 + 1 to n do
        if biin(a[n0] * 3 - a[i] - a[j]) then
          exit(true);
    libtbc := false
  end;

begin
  assign(f, 'TBC.INP');
  reset(f);
  readln(f, n);
  for i := 1 to n do readln(f, a[i]);
  close(f);
  qsort(1, n);
  m := 0;
  for l := 2 to n - 1 do
    if libtbc(l) then
      inc(m);
  assign(f, 'TBC.OUT');
  rewrite(f);
  writeln(f, m);
  close(f)
end.