#! /usr/bin/python3

import sys
import fileinput
import math

def do_proba(a, b, x, y):
    return (((a - x) * (b - y)) / (((5 * a) - 150) * ((5 * b) - 150)))

def print_usage():
    print("USAGE\n\t   ./202unsold a b\n")
    print("DESCRIPTION\n\t   a    constant computed from the past")
    print("\t   b    constant computed from the past")

def make_my_first_array(a, b):
    arr = []
    law_x = []
    y = 10
    x = 10
    jul = 0

    arr.append("\tX=10\tX=20\tX=30\tX=40\tX=50\tY law")
    for i in range(5):
        c = 10
        j = 0
        summit = 0
        law_y = do_proba(a, b, 10, y) + do_proba(a, b, 20, y) + do_proba(a, b, 30, y) + do_proba(a, b, 40, y) + do_proba(a, b, 50, y)
        for j in range(5):
            summit = do_proba(a, b, x, c) + summit
            c = c + 10        
        arr.append("Y=" + str(y) + "\t" + "%.3f" % (do_proba(a, b, 10, y)) + "\t" + "%.3f" % (do_proba(a, b, 20, y)) + "\t" + "%.3f" % (do_proba(a, b, 30, y)) + "\t" + "%.3f" % (do_proba(a, b, 40, y)) + "\t" + "%.3f" % (do_proba(a, b, 50, y)) + "\t" + "%.3f" % (law_y))
        law_x.append("%.3f" % (summit))
        jul = jul + summit  
        y = y + 10
        x = x + 10
    arr.append("X law" + "\t" + law_x[0] + "\t" + law_x[1] + "\t" + law_x[2] + "\t" + law_x[3] + "\t" + law_x[4] + "\t" + "%.0f" % (jul))
    return (arr)

def compute_z(a, b, z):
    if (z > 60):
        x = 50
        y = z - 50
    else:
        x = z - 10
        y = 10
    result = 0

    while (y < z and y <= 50):
        # if (z > 70):
        #     result += abs(do_proba(a, b, x, y))
        # else:
        result += do_proba(a, b, x, y)
        x -= 10
        y += 10
    return result

def make_my_second_array(a, b):
    arr = []
    

    arr.append("z\t20\t30\t40\t50\t60\t70\t80\t90\t100\ttotal")
    arr.append("p(Z=z)\t" + "%.3f\t" % (compute_z(a, b, 20)) + "%.3f\t" % (compute_z(a, b, 30)) + "%.3f\t" % (compute_z(a, b, 40))
                + "%.3f\t" % (compute_z(a, b, 50)) + "%.3f\t" % (compute_z(a, b, 60)) + "%.3f\t" % (compute_z(a, b, 70)) + "%.3f\t" % (compute_z(a, b, 80))
                + "%.3f\t" % (compute_z(a, b, 90)) + "%.3f\t" % (compute_z(a, b, 100)) + "1")


    return arr

def getExpected(a, b, l):
    y = 10
    x = 10
    tmp = 0
    result = 0

    if (l == "X"):
        while (x <= 50):
            while (y <= 50):
                tmp += do_proba(a, b, x, y)
                y += 10
            
            result += tmp * x
            y = 10
            x += 10
            tmp = 0

    if (l == "Y"):
        while (y <= 50):
            while (x <= 50):
                tmp += do_proba(a, b, x, y)
                x += 10
            
            result += tmp * y
            x = 10
            y += 10
            tmp = 0

    if (l == "Z"):
        tmp = 20

        while (tmp <= 100):
            result += tmp * compute_z(a, b, tmp)
            tmp += 10
    return result

def getVariance(a, b, ex, l):
    x = 10
    y = 10
    result = 0
    tmp = 0

    
    if l == "X":
        while x <= 50:
            while y <= 50:
                tmp += do_proba(a, b, x, y)
                y += 10
            result += (tmp * (x - ex) ** 2)
            x += 10
            y = 10
            tmp = 0
    if l == "Y":
        while y <= 50:
            while x <= 50:
                tmp += do_proba(a, b, x, y)
                x += 10
            result += (tmp * (y - ex) ** 2)
            y += 10
            x = 10
            tmp = 0
    if l == "Z":
        while (x <= 100):
            result += compute_z(a, b, x) * ((x - ex) ** 2)
            x += 10

    return result




def make_my_third_array(a, b, second):
    arr = []

    ex = getExpected(a, b, "X")
    arr.append("expected value of X:\t%.1f" % (ex))
    arr.append("variance of X:\t\t%.1f" % (getVariance(a, b, ex, "X")))
    ex = getExpected(a, b, "Y")
    arr.append("expected value of Y:\t%.1f" % (ex))
    arr.append("variance of Y:\t\t%.1f" % (getVariance(a, b, ex, "Y")))
    ex = getExpected(a, b, "Z")
    arr.append("expected value of Z:\t%.1f" % (ex))
    arr.append("variance of Z:\t\t%.1f" % (getVariance(a, b, ex, "Z")))

    return arr


def main():
    
    if len(sys.argv) < 2 or len(sys.argv) > 3:
        
        exit(84)
    if len(sys.argv) == 2:
        if sys.argv[1] == "-h":
            print_usage()
            exit (0)
        else:
            exit(84)
    if sys.argv[1].isdigit() == False or sys.argv[2].isdigit() == False:
        exit (84)
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    if a < 50 or b < 50:
        exit(84)
    arr = make_my_first_array(int(sys.argv[1]), int(sys.argv[2]))
    z =  make_my_second_array(int(sys.argv[1]), int(sys.argv[2]))
    # print("%.3f" % (do_proba(60, 70, 50, 10)))
    print("------------------------------------------------------")
    for i in range (7):
        print(arr[i])
    print("------------------------------------------------------")
    for i in range (2):
        print(z[i])
    print("------------------------------------------------------")
    arr = make_my_third_array(int(sys.argv[1]), int(sys.argv[2]), z)
    for i in range (6):
        print(arr[i])
    print("------------------------------------------------------")


if __name__ == "__main__":
    main()