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

public class TestGen {

    static int done = 0;

    static final Random rnd = new Random(556668949238L);

    static int rand(int n) {
        if (n < 1)
            return 0;
        return rnd.nextInt(n);
    }

    static final int MAX = 20;

    static int n, m, x, y, k;

    static PrintWriter desc;

    static void print() throws IOException {
        done++;
        System.err.println("Generation test #" + done);
        PrintWriter out;
        if (done < 10)
            out = new PrintWriter("../tests/00" + String.valueOf(done));
        else
        if (done < 100)
            out = new PrintWriter("../tests/0" + String.valueOf(done));
        else
            out = new PrintWriter("../tests/" + String.valueOf(done));
        out.println(n + " " + m);
        out.println(x + " " + y);
        out.println(k);
        out.close();
    }

    static String getPath() {
        if (done < 10)
            return "../tests/00" + String.valueOf(done);
        else
        if (done < 100)
            return "../tests/0" + String.valueOf(done);
        return "../tests/" + String.valueOf(done);
    }


    static void gen_samples() throws IOException {
        PrintWriter out;
        String path;
        path = "../preliminary/001";
        out = new PrintWriter(path);
        out.println("3 3");
        out.println("2 2");
        out.println("2");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 1");
        path = "../preliminary/002";
        out = new PrintWriter(path);
        out.println("4 4");
        out.println("2 2");
        out.println("3");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 2");
        path = "../preliminary/003";
        out = new PrintWriter(path);
        out.println("5 1");
        out.println("5 1");
        out.println("4");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 3");
        path = "../preliminary/004";
        out = new PrintWriter(path);
        out.println("7 5");
        out.println("4 3");
        out.println("3");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 4");
    }

    static void print_desc(String path, String folder, String subtask, int points, String comment) throws IOException {
        desc.println(String.format("%s, folder=%s, subtask=%s, points=%d, comment=\"%s\"", path, folder, subtask, points, comment));
    }

    static String subtask;

    static void print_desc_t(String comment) throws IOException {
        print_desc(getPath(), "tests", subtask, 1, comment);
    }

    //begin gens

    static void genSimple() throws IOException {
        n = m = x = y = k = 1;
        print();
        print_desc_t("everything is one");
    }

    static void genK1(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        x = rand(n) + 1;
        y = rand(m) + 1;
        k = 1;
        print();
        print_desc_t("k = 1");
    }

    static void swap(int a, int b) {
        int tmp = a;
        a = b;
        b = tmp;
    }

    static boolean coin() {
        return rand(2) == 1;
    }

    static void genCorner1(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        x = 1;
        y = 1;
        if (coin())
            x = n;
        if (coin())
            y = m;
        k = rand(Math.min(n, m) - 1) + 2;
        print();
        print_desc_t(String.format("Corner. n = %d m = %d, k = %d", n, m, k));
    }

    static void genCorner2(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        x = 1;
        y = 1;
        if (coin())
            x = n;
        if (coin())
            y = m;
        k = Math.min(n, m) + rand(Math.max(n, m) - Math.min(n, m)) + 1;
        print();
        print_desc_t(String.format("Corner with one way. n = %d m = %d, k = %d", n, m, k));
    }

    static void genBorder(int max) throws IOException {
        n = rand(max / 2) + max / 2;
        m = rand(max / 2) + max / 2;
        x = 1;
        if (coin())
            x = n;
        y = 2 + rand(m - 3);
        if (coin()) {
            swap(x, y);
            swap(n, m);
        }
        k = rand(Math.max(n, m) - 1) + 2;
        print();
        print_desc_t(String.format("Border. n = %d m = %d, k = %d", n, m, k));
    }

    static void genField(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        k = rand(Math.max(n, m) - 1) + 2;
        while (n < 2 * k - 1 && m < 2 * k - 1)
            k = rand(max) + 1;
        x = k + rand(n - k);
        y = k + rand(m - k);
        print();
        print_desc_t(String.format("far away from border. n = %d m = %d, k = %d", n, m, k));
    }

    static void genSpecial1(int max) throws IOException {
        n = rand(max / 2) + max / 2;
        m = rand(max / 2) + max / 2;
        k = rand(Math.max(n, m) - 1) + 2;
        while (n < 2 * k - 1)
            k = rand(max) + 1;
        x = k + rand(n - k);
        y = rand(k) + 1;
        while (y == 1 || y == m)
            y = rand(k) + 1;
        if (coin()) {
            swap(x, y);
            swap(n, m);
        }
        print();
        print_desc_t(String.format("near the border. n = %d m = %d, k = %d", n, m, k));
    }

    static void genSpecial2(int max) throws IOException {
        n = rand(max / 2) + max / 2;
        m = rand(max / 2) + max / 2;
        k = rand(Math.max(n, m) - 1) + 2;
        y = rand(k) + 1;
        while (y == 1 || y == m)
            y = rand(k) + 1;
        x = rand(k) + 1;
        while (x == 1 || x == n)
            x = rand(k) + 1;
        print();
        print_desc_t(String.format("near the border by two directions. n = %d m = %d, k = %d", n, m, k));
    }

    static void genSmallLength(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        k = Math.min(n, m) + rand(Math.max(n, m) - Math.min(n, m)) + 1;
        x = rand(n) + 1;
        y = rand(m) + 1;
        print();
        print_desc_t(String.format("one direction. n = %d m = %d, k = %d", n, m, k));
    }

    static void genSquare(int max) throws IOException {
        n = rand(max / 2) + max / 2;
        m = n;
        k = n;
        x = rand(n) + 1;
        y = rand(m) + 1;
        print();
        print_desc_t(String.format("n = m = k = %d", n));
    }

    static void genSpeial31(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        k = Math.min(n, m);
        x = rand(n) + 1;
        y = rand(m) + 1;
        print();
        print_desc_t(String.format("two way. n = k | m = k, k = %d", k));
    }

    static void genSpeial32(int max) throws IOException {
        n = rand(max) + 1;
        m = rand(max) + 1;
        k = Math.max(n, m);
        x = rand(n) + 1;
        y = rand(m) + 1;
        print();
        print_desc_t(String.format("one way. n = k | m = k, k = %d", k));
    }

    static void genSpeial41(int max) throws IOException {
        n = rand(max) + 1;
        m = 1;
        k = rand(n - 1) + 2;
        x = rand(n) + 1;
        y = 1;
        if (coin()) {
            swap(x, y);
            swap(n, m);
        }
        print();
        print_desc_t(String.format("n = 1 | m = 1, k = %d", k));
    }

    static void genSpeial42(int max) throws IOException {
        n = rand(max) + 1;
        m = 1;
        k = n;
        x = rand(n) + 1;
        y = 1;
        if (coin()) {
            swap(x, y);
            swap(n, m);
        }
        print();
        print_desc_t(String.format("n = 1 | m = 1, other = k, k = %d", k));
    }

    static void gen553() throws IOException {
        n = 5;
        m = 5;
        k = 3;
        for (x = 1; x <= 5; x++)
            for (y = 1; y <= 5; y++) {
                print();
                print_desc_t(String.format("5 5 %d %d 3", x, y));
            }
    }

    static void gen353() throws IOException {
        n = 3;
        m = 6;
        k = 3;
        for (x = 1; x <= 3; x++)
            for (y = 1; y <= 5; y++) {
                print();
                print_desc_t(String.format("3 5 %d %d 3", x, y));
            }
    }

    static void gen20() throws IOException {
        n = m = x = y = k = 20;
        print();
        print_desc_t(String.format("everything is 20"));
        for (int i = 1; i < 32; i++) {
            int[] test = {20, 20, 20, 20, 20};
            for (int j = 0; j < 5; j++) {
                int q;
                if (0 != (i & (1 << j)))
                    q = 1;
                else
                    q = 0;
                test[j] -= q;
            }
            n = test[0];
            m = test[1];
            x = test[2];
            y = test[3];
            k = test[4];
            if (n >= x && m >= y && (k <= n || k <= m)) {
                print();
                print_desc_t(String.format("%d %d %d %d %d", n, m, x, y, k));
            }
        }
    }

    static void genTrash() throws IOException {
    	n = m = 20;
    	x = y = 10;
    	k = 10;
    	print();
    	print_desc_t(String.format("20 20 10 10 10"));

		n = 20;
		m = 15;
		x = 10;
		y = 15;
		k = 9;
		print();
		print_desc_t("20 15 10 15 9");

		n = 15;
		m = 20;
		x = 1;
		y = 5;
		k = 11;
		print();
		print_desc_t("15 20 1 5 11");

		n = 17;
		m = 5;
		x = 10;
		y = 1;
		k = 7;
		print();
		print_desc_t("17 5 10 1 7");

		n = 17;
		m = 20;
		x = 10;
		y = 20;
		k = 18;
		print();
		print_desc_t("17 20 10 20 18");
    }

    //end gens

    public static void main(String[] args) throws IOException {
        new File("../preliminary").mkdir();
        new File("../tests").mkdir();
        desc = new PrintWriter("../tests.desc");

        done = 0;
        gen_samples();

        subtask = "subtask1";

        genSimple();
        genK1(20);
        for (int i = 0; i < 3; i++) {
            genCorner1(20);
            genCorner2(20);
            genBorder(20);
            genField(20);
            genSpecial1(20);
            genSpecial2(20);
            genSpeial31(20);
            genSpeial32(20);
            genSpeial41(20);
            genSpeial42(20);
            genSmallLength(20);
            genSquare(20);
        }
        gen553();
        gen353();
        gen20();
        genTrash();

        desc.println(String.format("comment_subtask%d=\"n &lt;= %d\"", 1, MAX));
        desc.println(String.format("bonus_subtask%d=%d", 1, 0));

        desc.close();
    }
}
