Cod sursa(job #24222)

Utilizator vladcyb1Vlad Berteanu vladcyb1 Data 1 martie 2007 22:10:22
Problema Triplete Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.37 kb

 const
      FIN = 'triplete.in';
      FOUT = 'triplete.out';
      NMAX = 4096;
      bucata = 15;


   var
       NBIT : array[ 0..1 shl 15 ] of longint;
       V : array[ 1..NMAX, 0..275 ] of integer;
       A, B : array[ 1..100000 ] of longint;
       f, g : text;
       N, M, ans : longint;

 procedure process;
  var i, j : longint;
  begin
   for i := 0 to 1 shl bucata do
     for j := 0 to bucata - 1 do
       if ( 1 shl j ) and ( i ) > 0 then inc( NBIT[i] );
  end;

 procedure add( x, y : longint ); // adauga nodul y la nodul x
  var buc, bit : longint;
  begin
    buc := y div bucata;
    bit := y mod bucata;
    V[x][buc] := V[x][buc] + ( 1 shl bit );
  end;

 procedure read_data;
  var i, x, y : longint;
  begin
   assign( f, FIN ); reset( f ); readln( f, N, M );
   for i := 1 to M do
     begin
      readln( f, x, y ); A[i] := x; B[i] := y;
      add( x, y );
      add( y, x );
     end;
  close( f );
  end;

  procedure solve;
   var i, j, tmax : longint;
   begin
    ans := 0;
    tmax := N div bucata;
     for  i := 1 to M do
       for j := 0 to tmax do ans := ans + NBIT[ V[A[i],j] and V[B[i],j ] ];
    end;

   procedure save;
    begin
     assign( g, FOUT ); rewrite( g );
     writeln( g, ans div 3);
     close( g );
    end;

   begin
    read_data;
    process;
    solve;
    save;
   end.