Product and quotient rules

Real formulas are usually products and ratios of simpler pieces, and the product and quotient rules handle them.

What to build

Given two polynomials u and v and a point x, print the derivative of u·v and of u÷v at x.

Requirements

  • Evaluate u, v, u′ and v′ at x exactly.
  • Apply the product rule and the quotient rule.
  • Print the product derivative as a whole number and the quotient derivative with 4 decimal places.

Input

Line 1: the coefficients of u (1 to 5 whole numbers, highest power first). Line 2: the coefficients of v (1 to 5 whole numbers). Line 3: a whole number x. It is guaranteed that v(x) is not 0.

Output

Two lines: product P with P a whole number, and quotient Q with Q to 4 decimal places.

Constraints

Coefficients and x are between -9 and 9.

Example

Input:

1 0
1 1
1

Output:

product 3
quotient 0.2500

Why it works

Both rules need only the four numbers u(x), v(x), u′(x) and v′(x). The product rule adds two terms, and the quotient rule divides by v². Using exact integers and fractions avoids rounding until the final print.

Examples

Example 1 · Input
1 0
1 1
1
Expected output
product 3
quotient 0.2500
Example 2 · Input
1 0 0
1 0 1
2
Expected output
product 36
quotient 0.1600

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

(uv)′ = u′v + uv′ and (u/v)′ = (u′v − uv′) ÷ v².

Hint 2

Write helpers to evaluate a polynomial and its derivative at x with Fraction or integers; the results are then exact.

Hint 3

Print the quotient with `:.4f`, converting a Fraction with float().

Solution

Show one correct solution (try it yourself first)
from fractions import Fraction

u = [int(c) for c in input().split()]
v = [int(c) for c in input().split()]
x = int(input())

def val(coeffs, t):
    total = 0
    for c in coeffs:
        total = total * t + c
    return total

def der(coeffs, t):
    n = len(coeffs) - 1
    return sum(c * (n - i) * t ** (n - i - 1) for i, c in enumerate(coeffs) if n - i > 0)

uu, vv, du, dv = val(u, x), val(v, x), der(u, x), der(v, x)
print("product", du * vv + uu * dv)
q = Fraction(du * vv - uu * dv, vv * vv)
print(f"quotient {float(q) + 0.0:.4f}")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Rules for derivatives →