This page is still under construction.

Parts of this page are still being built. What you see may change.

cubic

Time limit1sMemory limit512 MB

Summary
Given integer coefficients of a cubic, return every rational root, using the rational root theorem to test candidate divisors of the constant and leading terms.
Level

Medium5 of 10

Topics
Math, Number theory, Brute force, Implementation
Solved
No attempts yet

Problem

Write a function P7:

  • Input parameters: integers a, b, c, d satisfying 1≤max⁡(∣1\le\max(|a∣,∣|,|b∣,∣|,|c∣,∣|,|d∣)≤109|)\le10^9

  • Return value: a list of all rational roots of the equation ax3+x^3+bx2+x^2+cx+x+d=0=0, in any order. (Each root should be included only once.)

    • Suppose the exact answer is [t0,t1,⋯ ,tn−1][t_0,t_1,\cdots,t_{n-1}] and your output is [u0,u1,⋯ ,um−1][u_0,u_1,\cdots,u_{m-1}].
    • For every non-negative integer kk, let r(k)={i∈Z:0≤i\<k}={0,1,⋯ ,k−1}r(k)=\{i\in\mathbb Z:0\le i\<k\}=\{0,1,\cdots,k-1\}.
    • Your answer is graded correct if and only if there exists a bijective function σ ⁣:r(n)→r(m)\sigma\colon r(n)\to r(m) such that [\frac{|u_{\sigma(i)}-t_i|}{\max(1,|t_i|)}\le10^{-6}\qquad\text{for all }i\in r(n)]
  • Hint: The rational root theorem states the following.

    • Consider a polynomial f(x)=anxn+an−1xn−1+⋯+a0f(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots+a_0 with integer coefficients where ana_n and a0a_0 are nonzero.
    • If f(pq)=0\displaystyle f\left(\frac{p}{q}\right)=0, where ∣p∣|p| and ∣q∣|q| are relatively prime positive integers, then a0p\displaystyle\frac{a_0}{p} and anq\displaystyle\frac{a_n}{q} are integers.

Examples1

  1. Example 1

    Input
    1 0 0 0
    
    Expected output
    0