import java.applet.Applet;
import java.awt.*;
import java.util.Random;

public class Hash extends Applet 
  {
    static int N = 10000;
    static int dotsize = 4;
    static int dflag = 0;
    static Graphics g;

    static void clear(Color c)
      { 
        int h = g.getClipRect().height;
        int w = g.getClipRect().width;
        g.setColor(c);
        g.fillRect(0, 0, w, h);
      }
    static void paint(int i, int j, Color c)
      { 
        int h = g.getClipRect().height;
        int w = g.getClipRect().width;
        g.setColor(c);
        g.fillOval( (i*w)/N, (j*h)/N, dotsize, dotsize);
      }
    static void probe(int N, int t, int flag)
      { int i, j; 
        int[] a = new int[N];
        for (i = 0; i < N; i++) a[i] = t;
        for (i = 0; i < N-1; i++)
          { int d;
            int h = (int) (Math.random() * N);
            int hh = h;
            if (flag == 0) d = 1; else d = 1 + (int) (Math.random() * 5);
            if (flag < 0) d = -flag;
            paint(hh, t, Color.red);
            while (a[h] > t)
              {
                for (int k = 0; k < 500; k++)
                  paint(h, a[h], Color.blue);
                a[h]++;
                h = (h+d) % N;
              }         
            paint(hh, t, Color.black);
            paint(h, t, Color.black);
            a[h] = t+2;
          }
      }
    public void paint(Graphics page)
      {
        g = page;
        N = Integer.parseInt(getParameter("N"));
        dotsize = Integer.parseInt(getParameter("dotsize"));
        dflag = Integer.parseInt(getParameter("dflag"));
        if (dflag == 0)
          {
            probe(N, 0, 0);
            probe(N, N/4, 0);
            probe(N, N/2, 1);
            probe(N, N - N/3, 1);
            probe(N, N - N/6, 1);
          }
        if (dflag == -1)
          {
            probe(N, 0, 1);
            probe(N, N/4, 1);
            probe(N, N/2, 1);
            probe(N, N - N/4, 1);
          }
        if (dflag > 0)
            probe(N, 0, -dflag);
      }
  }

