Given three positive integers N, R, and A and a polynomial P(X) of degree N, P(i) = 1 for 1 ≤ i ≤ N and the value of P(N + 1) is A, the task is to find the value of the P(N + R).
Input: N = 1, R = 3, A = 2
The equation of the given polynomial is P(x) = x. Therefore, the value of P(N + R) = P(4) = 4.
Input: N = 2, R = 3, A = 1
Approach: The given problem can be solved by finding the expression of the given polynomial P(X) and then calculate the value of P(N + R). The general form of a polynomial with degree N is:
P(x) = k * (x – x1) * (x – x2) * (x – x3) * … *(x – xn) + c
where x1, x2, x3, …, xn are the roots P(x) where k and c are arbitrary constants.
Consider F(x) be another polynomial of degree N such that
F(x) = P(x) + c — (1)
Now, if c = -1, F(x) = 0 since P(x) = 1 for x ∈ [1, N].
⇒ F(x) has N roots which are equal to 1, 2, 3, …, N.
Therefore, F(x) = k * (x – 1) * (x – 2) * … * (x – N), for all c = -1.
Rearranging terms in equation (1),
P(x) = F(x) – c
P(x) = k * (x – 1) * (x – 2) * … *(x – N) + 1 — (2)
Putting x = N + 1 in equation (2),
k = (a – 1) / N!
Therefore, P(x) = ((a – 1)/N!) * (x – 1) * (x – 2) * … *(x – N) + 1 — (3)
Therefore, the idea is to substitute the value of (N + R) in equation (3) and print the value of the expression as the result. Follow the steps below to solve the problem:
- Initialize a variable, say ans, as the value of (A – 1) / N!.
- Iterate over the range [1, N] using the variable i and update the value of ans as ans * (N + R – i).
- After completing the above steps, print the value of (ans + 1) as the result.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.