import java.io.*;
import java.math.BigInteger;
import java.util.Random;

public class TestGen {
    int testCount, subtaskCount;
    PrintWriter testsDesc;

    Random random = new Random(787788);
    
    int[][] samples = new int[][]{new int[]{1, 1 ,2}, new int[]{1, 2, 2, 3, 1, 2}};

    void write(int[] a, String folder, String subtask, int points, String comment) throws IOException {
        String path = String.format("../%s/%02d", folder, ++testCount);
        PrintWriter out = new PrintWriter(path);
        out.println(a.length);
        for (int i = 0; i < a.length; i++) {
            out.print(a[i]);
            if (i + 1 < a.length) {
                out.print(" ");
            } else {
                out.println();
            }
        }
        out.close();
        testsDesc.println(String.format("%s, folder=%s, subtask=%s, points=%d, comment=%s", path, folder, subtask, points, comment));
    }


    void genPreliminary() throws IOException {
        for (int i = 0; i < samples.length; i++) {
            write(samples[i], "preliminary", "subtask0", 0, String.format("\"sample %d\"", i + 1));
        }
        testCount = 0;
    }

    void genRandom(int n, int k) throws IOException {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            if (k == 1) {
                a[i] = 100_000; // generate max input
            } else {
                a[i] = random.nextInt(k) + 1;
            }
        }
        write(a, "tests", "subtask" + Integer.toString(subtaskCount), 0, String.format("\"random, n=%d\"", n));
    }

    void genDifferent(int n) throws IOException {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = i + 1;
            int k = random.nextInt(i + 1);
            int o = a[k];
            a[k] = a[i];
            a[i] = o;
        }
        write(a, "tests", "subtask" + Integer.toString(subtaskCount), 0, String.format("\"all different, n=%d\"", n));
    }

    int f(int x) {
        int res = 0;
        while (x > 0) {
            if (x % 2 == 1) {
                res ^= 1;
            }
            x /= 2;
        }   
        return res;
    }

    void genMorseTue(int n) throws IOException {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = f(i) + 1;
        }
        write(a, "tests", "subtask" + Integer.toString(subtaskCount), 0, String.format("\"morse-tue, n=%d\"", n));
    }

    void genSubtask(int n, int points) throws IOException {
        ++subtaskCount;
        if (subtaskCount == 1) {
            genDifferent(1);
        }
        genRandom(n, 2);
        for (int k = 0; k < 5; k++) {
            int m = n - random.nextInt(n - n / 10);
            genRandom(m, 1 + random.nextInt(2 * (int)Math.sqrt(m)));
        }
        genRandom(n, (int)Math.sqrt(n));
        genRandom(n, n / 2);
        genRandom(n, n);
        genDifferent(n);
        genMorseTue(n);
        if (subtaskCount == 4) {
            int a[];
            a = new int[]{11, 12, 10, 3, 5, 6, 8, 4, 4, 1, 3, 9, 10, 10, 1, 8, 2, 5, 10, 13, 13, 1, 1, 12, 2, 9, 2, 11, 7, 10, 8, 6, 15, 12, 9, 12, 3, 10, 1, 9, 14, 10, 2, 13, 8, 6, 7, 4, 7, 5, 11, 12, 5, 2, 7, 8, 9, 1, 14, 11, 11, 15, 15, 2, 8, 6, 3, 9, 12, 7, 3, 1, 8, 13, 12, 10, 5, 6, 14, 12, 11, 9, 3, 11, 7, 4, 8, 9, 8, 5, 11, 3, 2, 11, 3, 15, 2, 2, 12, 1, 4, 7, 14, 2, 6, 6, 9, 12, 6, 11, 4, 8, 9, 14, 10, 12, 7, 9, 2, 7, 9, 8, 8, 4, 6, 10, 1, 10, 15, 10, 1, 8, 4, 9, 5, 14, 6, 15, 9, 15, 8, 11, 8, 5, 3, 5, 15, 14, 11, 7, 12, 10, 4, 6, 6, 13, 10, 4, 14, 2, 3, 3, 3, 9, 10, 4, 8, 10, 10, 12, 13, 13, 7, 7, 1, 14, 14, 2, 3, 13, 5, 14, 12, 13, 13, 12, 2, 13, 3, 10, 1, 13, 14, 13, 5, 2, 12, 11, 1, 9, 9, 8, 3, 3, 1, 6, 14, 7, 8, 4, 7, 11, 6, 13};
            write(a, "tests", "subtask" + Integer.toString(subtaskCount), 0, String.format("\"ans = 0; n = %d\"", a.length));
            a = new int[]{7, 15, 9, 3, 3, 12, 6, 13, 5, 12, 9, 2, 10, 15, 5, 3, 4, 3, 12, 3, 15, 4, 14, 2, 14, 2, 1, 13, 10, 12, 14, 15, 4, 11, 8, 1, 1, 3, 2, 13, 15, 7, 10, 15, 10, 3, 13, 4, 3, 10, 11, 1, 9, 5, 15, 8, 6, 9, 11, 12, 15, 3, 4, 2, 4, 12, 11, 7, 9, 4, 6, 8, 13, 15, 13, 10, 15, 13, 10, 11, 1, 2, 11, 5, 15, 8, 8, 1, 3, 8, 14, 15, 9, 3, 3, 10, 5, 4, 15, 13, 14, 15, 12, 15, 3, 9, 9, 12, 14, 11, 6, 9, 10, 13, 2, 10, 12, 1, 14, 6, 3, 1, 7, 6, 12, 13, 11, 15, 12, 15, 9, 9, 3};
            write(a, "tests", "subtask" + Integer.toString(subtaskCount), 0, String.format("\"ans = 10000000006; n = %d\"", a.length));

        }
        
        testsDesc.println(String.format("comment_subtask%d=\"n &lt;= %d\"", subtaskCount, n));
        testsDesc.println(String.format("bonus_subtask%d=%d", subtaskCount, points));       
    }

    void run() throws IOException {
        new File("../preliminary").mkdir();
        new File("../tests").mkdir();
        testsDesc = new PrintWriter("../tests.desc");

        genPreliminary();
        genSubtask(10, 30);
        genSubtask(300, 20);
        genSubtask(2000, 10);
        genSubtask(100000, 40);

        testsDesc.close();
    }

    public static void main(String[] args) throws IOException {
        new TestGen().run();
    }
}
