Editorial for Angie and Functions


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: Plasmatic

Subtasks 1 and 2

Subtasks 1 and 2 were intended to reward brute force solutions.

Time Complexity: O(10N)

Query Complexity: O(1)

Subtask 3

Official Solution

Observe that the Nth difference of an N degree polynomial is equivalent to its Nth derivative.

First, query any N+1 adjacent values.

Now, calculate the Nth difference of the original function, this can be done in O(N2) time using brute force. Then, find the function for the Nth derivative using the power rule and finally solve for the first coefficient (c1) using algebra. Note that modular inverse is required for this part.

Lastly, subtract c1xN from all of the queried values, this turns f(x) into c2xN1+c3xN2++cNx+cN+1. As the polynomial has now been reduced to an N1 degree polynomial the above process can be repeated to find c2,c3,.

Time Complexity: O(N3)

Query Complexity: O(N)

Alternative Solution

Observe that any N values can be queried to set up a system of N equations to solve for the coefficients. The coefficients can be found using Gaussian elimination in O(N3).

Time Complexity: O(N3)

Query Complexity: O(N)

Subtask 4

Official Solution

To get the full solution, simply optimize the method from the previous subtask. Observe that the Nth difference of a function follows a regular pattern, allowing the Nth difference to be calculated in O(NlogN) time. This brings the overall complexity down to O(N2logN).

The pattern:

Δx=f(x)f(x1)Δ2x=ΔxΔ(x1)=f(x)2f(x1)+f(x2)Δ3x=Δ2xΔ2(x1)=f(x)3f(x1)+3f(x2)f(x3)Δ4x=Δ3xΔ3(x1)=f(x)4f(x1)+6f(x2)4f(x3)+f(x4)

The pattern follows Pascal's triangle.

Time Complexity: O(N2logN) or O(N2)

Query Complexity: O(N)

Alternative Solution

Alternatively, observe that if adjacent values are queried, the matrix that is created follows certain patterns. These patterns can be exploited to improve the overall time complexity.

Interestingly, the resulting code is almost identical to that of the official solution.

Time Complexity: O(N2logN) or O(N2)

Query Complexity: O(N)

Alternative Solution 2

Lagrange Polynomial


Comments


  • 6
    crackersamdjam  commented on March 21, 2020, 1:37 p.m. edited

    Why don't you mention the O(N2) solution? https://en.wikipedia.org/wiki/Lagrange_polynomial


    • 2
      Plasmatic  commented on March 21, 2020, 6:06 p.m.

      The solution didn't seem interesting so I didn't include it originally, seeing as it was just copy paste some code/implement an equation. However, it was a pretty big oversight when I first wrote the problem.


    • -11
      666245  commented on March 21, 2020, 5:10 p.m. edited

      This comment is hidden due to too much negative feedback. Show it anyway.


      • 10
        zxyl  commented on March 21, 2020, 6:13 p.m.

        I challenge you to implement that solution :)


        • -6
          pblpbl  commented on March 22, 2020, 3:26 a.m.

          This comment is hidden due to too much negative feedback. Show it anyway.


  • 3
    harry7557558  commented on March 21, 2020, 7:50 a.m. edited

    Precalculating Pascal's triangle only require O(N2) time.