import java.io.*;
import java.util.*;

public class Gen {
    private static int MAX_N = 1000;
    private static int MAX = 1000000000;

    static int tests = 1;

    static int n, d;
    static int[] a;

    static Random rnd = new Random(239);

    public static void writeTest() throws IOException {
      System.out.println("Writing " + tests + " test!");

      PrintWriter out = new PrintWriter(String.format("../tests/%02d", tests++));
      out.println(n + " " + d);
      out.print(a[0]);
      for (int i = 1; i < n; i++) {
        out.print(" " + a[i]);
      }
      out.println();
      out.close();
    }

    public static int gcd(int a, int b) {
      if (b == 0)
        return a;
      else
        return gcd(b, a % b);
    }

    public static void gen(int k) throws IOException {
      n = k;
      d = rnd.nextInt(MAX) + 1;

      a = new int[n];
      int gcd = 0;
      for (int i = 0; i < n; i++) {
        int b = rnd.nextInt(MAX);
        if (b % d != 0 && rnd.nextBoolean()) {
          a[i] = b;
          continue;
        }

//        while (gcd(b, gcd) != gcd && rnd.nextBoolean())
        b = d * (rnd.nextInt(MAX / d) + 1);

        gcd = gcd(gcd, b);
        a[i] = b;
      }
      writeTest();
    }

    public static void genFail(int k) throws IOException {
      n = k;
      d = rnd.nextInt(MAX) + 1;

      a = new int[n];
      int gcd = 0;
      for (int i = 0; i < n; i++) {
        int b = rnd.nextInt(MAX);
        while (b % d == 0 && gcd(b, gcd) == gcd) {
          b = rnd.nextInt(MAX);
        }
        if (b % d == 0)
          gcd = gcd(b, gcd);
        a[i] = b;
      }

      writeTest();
    }

    public static void main(String[] args) throws IOException {
        File dir = new File("../tests");
        if (!dir.exists())
          dir.mkdir();

        n = 4;
        d = 3;
        a = new int[]{6, 8, 12, 9};
        writeTest();

        n = 3;
        d = 3;
        a = new int[]{2, 4, 8};
        writeTest();

	for (int i = 0; i < 5; i++) {
	  gen(rnd.nextInt(MAX_N / 100) + 1);
        }        
	for (int i = 0; i < 3; i++) {
	  genFail(rnd.nextInt(MAX_N / 100) + 1);
        }                      
        for (int i = 0; i < 10; i++) {
          gen(rnd.nextInt(MAX_N) + 1);
        }
        for (int i = 0; i < 5; i++) {
          genFail(rnd.nextInt(MAX_N) + 1);
        }
        for (int i = 0; i < 5; i++) {
          gen(MAX_N);
        }
        for (int i = 0; i < 3; i++) {
          genFail(MAX_N);
        }
    }
}