import random
import math
random.seed("fucking_good_seed")

class TestGen:
    def __init__(self):
        self.test_number = 0
        self.desc_file = open("../tests.desc", "w")

    def generate_samples(self):
        self.print_test(8, 2, "subtask0", "test from sample", 0, "preliminary")
        self.print_test(5, 3, "subtask0", "test from sample", 0, "preliminary")

    def generate_random_test_with_no_answer(self, maxn, maxk, subtask):
        #n < k or n = k + 1 or n % 2 = 1
        n = 0
        k = 0
        x = random.randint(1, 3)
        if x == 1:
            k = random.randint(2, maxk)
            n = random.randint(1, k - 1)
        elif x == 2:
            k = random.randint(1, min(maxn - 1, maxk))
            n = random.randint(k + 1, maxn)
        else:
            n = 2 * random.randint(0, (maxn - 1) // 2) + 1
            k = random.randint(1, maxk)
        self.print_test(n, k, subtask, "random test with no answer, n = %d, k = %d" % (n, k))

    def generate_max_test(self, f, maxn, maxk, subtask):
        pass

    def generate_first_group(self):
        maxn = 100000
        for n in range(2, 10, 2):
            self.print_test(n, 2, "subtask1", "min test, n = %d, k = 2" % (n))
        for i in range(5):
            x = random.randint(1, 50000) * 2
            self.print_test(x, 2, "subtask1", "n = %d, k = 2" % (x))
        self.print_test(100000, 2, "subtask1", "n = 100000, k = 2")
        self.print_test(99999, 2, "subtask1", "n = 99999, k = 2")
        self.print_test(1, 2, "subtask1", "n = 1, k = 2")

    def generate_second_group(self):
        maxn = 100000
        maxk = 100000

        #good tests
        for i in range(10):
            k = random.randint(1, 5) * 2
            n = 0
            if random.randint(1, 2) == 1:
                n = k
            else:
                n = random.randint(k + 2, maxn)
                while n % 2 == 1:
                    n = random.randint(k + 2, maxn)
            self.print_test(n, k, "subtask2", "random test, n = %d, k = %d" % (n, k))
        for i in range(10):
            k = random.randint(1, maxk // 2) * 2
            n = 0
            if random.randint(1, 2) == 1:
                n = k
            else:
                n = random.randint(k + 2, maxn)
                while n % 2 == 1:
                    n = random.randint(k + 2, maxn)
            self.print_test(n, k, "subtask2", "random test, n = %d, k = %d" % (n, k))

        #no answer
        for i in range(10):
            k = random.randint(1, maxk // 2) * 2
            x = random.randint(1, 3)
            if x == 1:
                n = random.randint(1, k - 1)
            elif x == 2:
                n = k + 1
            else:
                n = random.randint(k + 2, maxn)
                while n % 2 == 0:
                    n = random.randint(k + 2, maxn)
            self.print_test(n, k, "subtask2", "random test with no answer, n = %d, k = %d" % (n, k))

        self.print_test(53340, 12, "subtask2", "n = 53340, k = 12")
        self.print_test(53340, 28, "subtask2", "n = 53340, k = 28")

    def generate_third_group(self):
        #first and second groups
        self.print_test(50000, 2, "subtask3", "n = 50000, k = 2")
        self.print_test(49999, 2, "subtask3", "n = 49999, k = 2")
        self.print_test(53340, 8, "subtask3", "n = 53340, k = 8")
        self.print_test(53340, 12, "subtask3", "n = 53340, k = 12")
        self.print_test(53340, 28, "subtask3", "n = 53340, k = 28")
        
        maxn = 100000
        maxk = 100000
        #k > 3
        for i in range(15):
            k = random.randint(1, (maxk - 1) // 2) * 2 + 1
            n = random.randint(k + 2, maxn)
            while n % 2 == 1:
                n = random.randint(k + 2, maxn)
            self.print_test(n, k, "subtask3", "random test, n = %d, k = %d" % (n, k))
        
        #k = 3
        self.print_test(6, 3, "subtask3", "n = 6, k = 3")
        for i in range(15):
            n = random.randint(3, maxn // 2) * 2
            k = 3
            self.print_test(n, k, "subtask3", "n = %d, k = 3" % (n))
        for i in range(5):
            power = random.randint(1, 16)
            self.print_test(2**power, 3, "subtask3", "n = %d, k = 3" % (2**power))

        #wrong answer tests
        for i in range(10):
            self.generate_random_test_with_no_answer(maxn, maxk, "subtask3")

    def print_test(self, n, k, subtask, description, points=0, folder="tests"):
        self.test_number += 1
        print("Generating test %s\%d" % (subtask, self.test_number))
        test_name = "../" + folder + "/{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(n, k, file=test_file)
        print(test_name + ", folder=" + folder + ", subtask=" + subtask + ", points=" + str(points) + ", comment=\"" + description + "\"", file=self.desc_file)
        test_file.close()                                                                                             

    def generate_all_tests(self):
        self.generate_samples()
        self.test_number = 0

        #First group of tests
        self.generate_first_group()       #tests 01-##
        print("comment_subtask1=\"k = 2\"", file=self.desc_file)
        print("bonus_subtask1=20", file=self.desc_file)

        #Second group of tests
        self.generate_second_group()      #tests ##-##
        print("comment_subtask2=\"k is even\"", file=self.desc_file)
        print("bonus_subtask2=30", file=self.desc_file)
        
        #Third group of tests
        self.generate_third_group()       #tests ##-##
        print("comment_subtask3=\"n, k &lt;= 100000\"", file=self.desc_file)
        print("bonus_subtask3=50", file=self.desc_file)

        self.desc_file.close()

import os, shutil
if os.path.exists('../preliminary'):
    print('Directory preliminary already exists')
    shutil.rmtree('../preliminary', ignore_errors=False, onerror=None)
if os.path.exists('../tests'):
    print('Directory tests already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
    os.makedirs('../preliminary')
    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)
