import os, shutil
from random import *
import random
seed("grievous")

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

    def generate_random_array(self, n, maxvalue):
        a = []
        for i in range(n):
            a.append(random.randint(1, maxvalue))
        return a

    def generate_samples(self):
        self.print_test(3, 3, [1, 3, 2])
        self.print_test(3, 2, [1, 2, 3])

    def generate_min_tests(self):
        self.print_test(1, 1, [1])
        self.print_test(1, 1, [2])
        self.print_test(1, 2, [2])
        self.print_test(2, 1, [4, 1]) 
        self.print_test(2, 2, [1, 3])
        self.print_test(2, 2, [3, 1])
        self.print_test(2, 4, [2, 2])
        self.print_test(2, 5, [2, 2])

    def generate_max_tests(self, maxn, maxk, maxvalue):
        a = []
        for i in range(maxn):
            a.append(1)
        #self.print_test(maxn, maxk, a)
        self.print_test(maxn, 2 * maxn, a)

        for i in range(5):
            a = self.generate_random_array(maxn, maxvalue)
            self.print_test(maxn, 2 * maxn, a)
        

    def generate_hand_tests(self):
        self.print_test(3, 5, [2, 3, 4])
        self.print_test(4, 5, [3, 2, 1, 3])
                                 
    def generate_random_test(self, maxn, maxk, maxvalue):
        n = random.randint(1, maxn)
        k = random.randint(n, n * 2)
        a = self.generate_random_array(n, maxvalue)
        self.print_test(n, k, a)

    def generate_small_test(self):
        self.generate_random_test(10, 20, 10)

    def generate_medium_test(self):
        self.generate_random_test(200, 400, 1000)

    def generate_test_with_impossible_answer(self, maxn, maxk, maxvalue):
        n = random.randint(1, maxn)
        if maxk > n * 2:
            k = n * 2 + random.randint(1, maxk - n * 2)
            a = self.generate_random_array(n, maxvalue)
            self.print_test(n, k, a)

        n = random.randint(2, maxn)
        k = random.randint(1, min(maxk, n - 1))
        a = self.generate_random_array(n, maxvalue)
        self.print_test(n, k, a)
                         
    def print_test(self, n, k, a):
        self.test_number += 1
        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(element) for element in a), file=test_file)
        test_file.close()

    def generate_all_tests(self):
        maxn = 500
        maxk = 10**9
        maxvalue = 10**7
        self.generate_samples()
        self.generate_min_tests()
        self.generate_hand_tests()
        for i in range(15):
          self.generate_small_test()
        #for i in range(15):
        #  self.generate_medium_test()
        for i in range(15):
          self.generate_random_test(maxn, maxn * 2, maxvalue)
        for i in range(2):
          self.generate_test_with_impossible_answer(maxn, maxk, maxvalue)
        self.generate_max_tests(maxn, maxk, maxvalue)

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

random.seed(239017)
writer = SandwichesTestGen()
writer.generate_all_tests()
