Editorial for RGPC '17 P3 - Intercept


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: chenj

The conceptually simplest method of expanding using the distributive property will be described here. Firstly, we can store the coefficients of the terms in arrays. Save the first factor in an array \{1, -x_0\} (factors of polynomial functions take the form (x - d), and in this case, the coefficient of x will always be 1), and then iterate through the factors.

For each factor, call the function \text{Expand}(A, B), where A is the previous result of expansion (or the first array), and B is the new array \{1, -x_i\}. In this function, we will use a nested for loop, where the outer loop iterates through A and the inner loop iterates through B. In each iteration, multiply A_i by B_j, and then add that value to a third array C (the resulting expression) at the position i + j. Note that the length of C should be greater than A and B, depending on their degrees.

Lastly, to determine the leading coefficient, substitute the values of the known point into the function. The recommended way of doing this would be to substitute the known x value as you loop through the terms, then dividing the known y value by the result, because substituting into standard form and using pow() may result in floating-point inaccuracies. Remember to multiply all coefficients by the leading coefficient before outputting.

Time complexity: \mathcal{O}(Q \times N^2)


Comments

There are no comments at the moment.