POJ 3844(同余)

果断同余……

D[j]-D[i]  mod  k =0

D[j]=D[i]

求有多少相等数对,用队列O(n)


Program P3844;
const
   maxn=50000;
   maxd=1000000;
var
   ans,t,f,n,i,j:longint;
   d:array[0..maxn] of longint;
procedure qsort(l,r:longint);
var
   i,j,m,p:longint;
begin
   i:=l;j:=r;
   m:=d[(l+r) shr 1];
   repeat
      while d[i]<m do inc(i);
      while d[j]>m do dec(j);
      if i<=j then
      begin
         p:=d[i];
         d[i]:=d[j];
         d[j]:=p;
         inc(i);dec(j);
      end;

   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);

end;
begin
   read(t);
   d[0]:=0;
   while (t>0) do
   begin
      read(f,n);
      for i:=1 to n do
      begin
         read(d[i]);
         d[i]:=(d[i]+d[i-1]) mod f;
      end;
      qsort(1,n);

      ans:=0;
      i:=0;j:=0;
      while (i<=n) do
      begin
         while (d[i]=d[j]) and (j<n) do inc(j);
         if d[i]<>d[j] then dec(j);
         inc(ans,((j-i+1)*(j-i)) shr 1);
         i:=j+1;
         inc(j);
      end;


      writeln(ans);

      dec(t);
   end;
end.


你可能感兴趣的:(POJ 3844(同余))