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

public class TestGen {
	Random rnd = new Random(239);
	int tests = 1;
	int MAXN = 100000;
	int MAXM = 10;
	int MAX = 1000000000;

	public class Person implements Comparable<Person> {
		int a, b;
		int t;
		int id;

		public Person(int id, int t, int a, int b) {
			this.id = id;
			this.t = t;
			this.a = a;
			this.b = b;
		}

		public int compareTo(Person p) {
			return t == p.t ? id - p.id : Long.signum(t - p.t);
		}

		public String toString() {
			return t + " " + a + " " + b;
		}
	}

	public void writeTest(int n, int m, Person[] p) {
		try {
			PrintWriter out = new PrintWriter(String.format("../tests/%02d",
					tests++));

			out.println(n + " " + m);
			for (int i = 0; i < p.length; i++) {
				out.println(p[i]);
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void genFixed(int n, int m, int MAX) {
		m = Math.min(n, m);

		Person[] p = new Person[n];
		for (int i = 0; i < p.length; i++) {
			p[i] = new Person(i, 0, 0, 0);
		}

		int MAX_DIFF = Math.max((int) (1L * MAX * m / n), 1);

		TreeSet<Integer> ts = new TreeSet<Integer>();
		for (int i = 0; i < p.length; i++) {
			if (ts.size() == m) {
				int tf = ts.pollFirst();
				int diff = rnd.nextInt(MAX_DIFF) + 1;
				if (rnd.nextBoolean()) {
					p[i].t = Math.max(tf + rnd.nextInt(diff), p[i - 1].t);
					p[i].a = tf + diff - p[i].t;
					ts.add(p[i].t + p[i].a);
				} else {
					p[i].t = Math.max(rnd.nextInt(tf + 1) + 1, p[i - 1].t);
					p[i].a = diff;
					ts.add(tf + p[i].a);
				}
			} else {
				int diff = rnd.nextInt(Math.min(MAX_DIFF, 1000));//
				p[i].t = (i == 0 ? 1 : p[i - 1].t) + diff;
				p[i].a = rnd.nextInt(MAX_DIFF - diff) + 1;
			}
		}

		for (int i = 0; i < p.length; i++) {
			p[i].b = rnd.nextInt(MAX) + 1;
		}

		writeTest(n, m, p);
	}

	public void genRandom(int n, int m, int MAX) {
		Person[] p = new Person[n];
		for (int i = 0; i < p.length; i++) {
			p[i] = new Person(0, rnd.nextInt(MAX) + 1, rnd.nextInt(MAX) + 1,
					rnd.nextInt(MAX) + 1);
		}

		Arrays.sort(p);

		writeTest(n, m, p);
	}

	public void gen2Queue(int n, int m, int MAX, int prob) {
		Person[] p = new Person[n];

		for (int i = 0; i < p.length; i++) {
			if (rnd.nextInt(prob) == 0) {
				p[i] = new Person(0, rnd.nextInt(MAX) + 1, 0, 0);
			} else {
				if (rnd.nextBoolean()) {
					p[i] = new Person(0, rnd.nextInt(MAX) + 1,
							rnd.nextInt(MAX + 1), 0);
				} else {
					p[i] = new Person(0, rnd.nextInt(MAX) + 1, 0,
							rnd.nextInt(MAX + 1));
				}
			}
		}

		Arrays.sort(p);

		writeTest(n, m, p);
	}

	public void gen() {
		if (!new File("../tests").exists()) {
			new File("../tests").mkdir();
		}

		writeTest(4, 2, new Person[] { new Person(0, 1, 3, 3),
				new Person(1, 1, 2, 2), new Person(2, 2, 2, 1),
				new Person(3, 2, 1, 4) });
				                                   
		for (int i = 0; i < 2; i++) {
			genRandom(rnd.nextInt(10) + 1, rnd.nextInt(10) + 1,
					rnd.nextInt(20) + 1);
			int n = rnd.nextInt(10) + 1;
			genFixed(n, rnd.nextInt(10) + 1, rnd.nextInt(100) + 1);
			genFixed(rnd.nextInt(10) + 1, rnd.nextInt(10) + 1,
					rnd.nextInt(100) + 1);
		}

		for (int i = 0; i < 3; i++) {
			genRandom(rnd.nextInt(1000) + 1, rnd.nextInt(10) + 1, 1000000);
			genFixed(rnd.nextInt(1000) + 1, rnd.nextInt(10) + 1, 1000000);
		}

		for (int i = 0; i < 3; i++) {
			int n = rnd.nextInt(MAXN) + 1;
			genRandom(n, rnd.nextInt(10) + 1, MAX);
			n = rnd.nextInt(MAXN) + 1;
			genFixed(n, rnd.nextInt(10) + 1, rnd.nextInt(n));
			n = rnd.nextInt(MAXN) + 1;
			genFixed(n, rnd.nextInt(10) + 1, MAX);
		}

		genFixed(100000, 10, 1000000);
		for (int i = 0; i < 3; i++) {
			genFixed(MAXN, rnd.nextInt(MAXM) + 1, MAX);
			genRandom(MAXN, rnd.nextInt(MAXM) + 1, MAX);
		}

		genFixed(MAXN, MAXM, MAX);
		genRandom(MAXN, MAXM, MAX);
	}

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