import random
random.seed("spb-team-2015/traffic")

class TestGen:
    def __init__(self):
        self.test_number = 0

    def generate_samples(self):
        self.print_test(3, 4, [1, 2, 4])
        self.print_test(3, 4, [1, 2, 6])
        
    def generate_min_tests(self):
        self.print_test(1, 1, [1])
        self.print_test(1, 1, [100])
        self.print_test(1, 2, [1])
        self.print_test(1, 2, [99])
        self.print_test(1, 2, [100])
        self.print_test(2, 2, [2, 1])
        self.print_test(2, 3, [5, 4])
        self.print_test(2, 13, [100, 99])
        self.print_test(1, 1, [10**5])
        self.print_test(1, 2, [10**5 - 1])
        self.print_test(1, 2, [10**5])
        self.print_test(2, 13, [10**5, 10**5 - 1])
        self.print_test(2, 79, [10**5, 10**5 // 2])
        self.print_test(2, 83, [10**5, 10**5 // 2 + random.randint(1, 10**5 // 2)])

    def generate_hand_tests(self):
        self.print_test(5, 37, [100, 99, 100, 98, 98])
        self.print_test(7, 67, [67, 1, 100, 87, 39, 2, 97])

    def generate_random_test(self, maxn, maxk, maxcars):
        n = maxn
        k = random.randint(n, maxk)
        cars = [0] * n
        for i in range(n):
            cars[i] = random.randint(maxcars // 2, maxcars)
        self.print_test(n, k, cars)

    def generate_max_tests(self, maxn, maxk, maxcars):
        n = maxn
        k = maxn
        self.print_test(n, k, [maxcars] * n)

    def print_test(self, n, k, cars):
        self.test_number += 1
        print('Generating test #%d' % self.test_number)

        test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(n, k, file=test_file)
        print(' '.join(str(car) for car in cars), file=test_file)
        test_file.close()

    def generate_all_tests(self):
        self.generate_samples()
        self.generate_min_tests()
        self.generate_hand_tests()
        
        maxn = 300
        maxk = 300
        maxcars = 100000
        values = [maxn // 100, maxn // 30, maxn // 10, maxn // 5, maxn // 2, maxn]
        for n in values:
            for i in range(3):
                self.generate_random_test(n, maxk, maxcars)
        self.generate_max_tests(maxn, maxk, maxcars)

import os, shutil
if os.path.exists('../tests'):
    print('Directory <<tests>> already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
    os.makedirs('../tests')
except OSError:
    pass

import time
start = time.time()
writer = TestGen()
writer.generate_all_tests()
finish = time.time()
print('All tests were generated. Elapsed time: ', finish - start)
