#P16056. [CSPro 31] 梯度求解

[CSPro 31] 梯度求解

Background

Luogu’s testdata is only for non-official community communication and is not official testdata. Official judging link: https://www.cspro.org/.

The Xixiaifu Island operating company has recently been strongly promoting an intelligent municipal management system. This system was developed by the Xixiaifu Island Information Center. Its main purpose is to guide the maintenance and renewal of municipal facilities by carefully evaluating the condition of municipal facilities across the island. The core of this system is an intelligent sensor network, which can automatically evaluate the island’s municipal facilities. Maintaining municipal facilities requires certain costs, and municipal facilities that have been neglected for years may also cause losses to the island’s residents. To balance costs and benefits, the Information Center developed a mathematical model that describes the complex mathematical relationship among these variables and the profit/loss. To obtain the optimal cost, it is necessary to rely on the gradient descent algorithm to solve it.

In the gradient descent algorithm, computing the partial derivative of a function at a point with respect to a certain variable is very important. Little C is responsible for implementing this function, but he is still confused about the technical details and hopes you can help him complete this task.

Problem Description

Let the function to be computed be u=f(x1,x2,…,xn)u = f(x_1, x_2, \dots, x_n). This problem asks you to compute the partial derivative of uu with respect to xix_i at (a1,a2,…,an)(a_1, a_2, \dots, a_n), i.e. $\frac{\partial u}{\partial x_i}(a_1, a_2, \dots, a_n)$.

To compute the partial derivative of a multivariable function at a point with respect to one variable: treat that variable as the only variable and treat the other variables as constants, use the differentiation method for single-variable functions to obtain the expression of the partial derivative, and then substitute the coordinates of the given point.

For example, to compute the partial derivative of u=x1⋅x1⋅x2u = x_1 \cdot x_1 \cdot x_2 with respect to x1x_1 at (1,2)(1, 2), you can treat x2x_2 as a constant and apply differentiation formulas step by step. First apply the product rule: $(x_1 \cdot (x_1 \cdot x_2))' = x_1'(x_1 \cdot x_2) + x_1(x_1 \cdot x_2)'$; then apply the rule for multiplying a constant and a variable to get $x_1' \cdot x_1 \cdot x_2 + x_1 \cdot x_2 \cdot x_1'$; finally apply x′=1x' = 1 to get 1⋅x1⋅x2+x1⋅x2⋅11 \cdot x_1 \cdot x_2 + x_1 \cdot x_2 \cdot 1. After simplification, ∂u∂x1=2x2⋅x1\frac{\partial u}{\partial x_1} = 2x_2 \cdot x_1. Substituting (1,2)(1, 2) gives ∂u∂x1(1,2)=4\frac{\partial u}{\partial x_1}(1, 2) = 4.

Common differentiation formulas include:

  • c′=0c' = 0 (cc is a constant).
  • x′=1x' = 1.
  • (u+v)′=u′+v′(u + v)' = u' + v'.
  • (cu)′=cu′(cu)' = cu' (cc is a constant).
  • (u−v)′=u′−v′(u - v)' = u' - v'.
  • (uv)′=u′v+uv′(uv)' = u'v + uv'.

In this problem, the function ff you need to compute consists only of constants, variables, and their addition, subtraction, and multiplication. For easier parsing, the function expression has been converted into Reverse Polish Notation (postfix notation). For example, the Reverse Polish Notation of x1⋅x1⋅x2x_1 \cdot x_1 \cdot x_2 is x1 x1 * x2 *. Reverse Polish Notation is the result of a postorder traversal of the expression tree. To restore the original expression from Reverse Polish Notation, you can do the following: assume there is an empty stack SS. Read each element of the Reverse Polish Notation in order. If it is a variable or a constant, push it into SS; if it is an operator, pop two elements from SS, perform the corresponding operation, and then push the result back into SS. Finally, if there is exactly one element in SS, the expression is valid and its value is the value of that element. For example, for x1 x1 * x2 *, reading it with the method above, the changes of stack SS are as follows (left is the bottom of the stack, right is the top):

  1. x1x_1.
  2. x1, x1x_1,\ x_1.
  3. (x1⋅x1)(x_1 \cdot x_1).
  4. (x1⋅x1), x2(x_1 \cdot x_1),\ x_2.
  5. ((x1⋅x1)⋅x2)((x_1 \cdot x_1) \cdot x_2).

Input Format

Read from standard input.

The first line contains two positive integers nn and mm separated by spaces, representing the number of variables in the target function and the number of partial derivatives to be computed.

The second line is a Reverse Polish Notation expression representing the function ff. Each element is separated by a space, and each element can be:

  • A variable xix_i, written as the character x followed by a positive integer, representing the ii-th variable, where i=1,2,…,ni = 1, 2, \dots, n. For example, x1 represents the first variable x1x_1.
  • An integer constant written in decimal, with value between −105-10^5 and 10510^5.
  • An operator: + for addition, - for subtraction, * for multiplication.

From the third line to the (m+2)(m + 2)-th line, each line contains n+1n + 1 integers separated by spaces. The first integer is the index i=1,2,…,ni = 1, 2, \dots, n of the variable for which the partial derivative is requested, and the remaining integers are the coordinates a1,a2,…,ana_1, a_2, \dots, a_n of the point to be evaluated. The input guarantees that for all i=1,2,…,ni = 1, 2, \dots, n, aia_i is between −105-10^5 and 10510^5.

Output Format

Output to standard output.

Output mm lines. Each line contains one integer, which is the corresponding partial derivative modulo 109+710^9 + 7. That is, if the result is yy and the output is kk, then there exists an integer tt such that y=k+t⋅(109+7)y = k + t \cdot (10^9 + 7) and 0≤k<109+70 \le k < 10^9 + 7.

2 2
x1 x1 x1 * x2 + *
1 2 3
2 3 4
15
3
3 5
x2 x2 * x2 * 0 + -100000 -100000 * x2 * -
3 100000 100000 100000
2 0 0 0
2 0 -1 0
2 0 1 0
2 0 100000 0
0
70
73
73
999999867

Hint

Sample 1 Explanation

Reading the Reverse Polish Notation, the expression to be differentiated is: u=x1⋅(x1⋅x1+x2)u = x_1 \cdot (x_1 \cdot x_1 + x_2), i.e. u=x13+x1x2u = x_1^3 + x_1 x_2.

Taking the partial derivative with respect to x1x_1, we get ∂u∂x1=3x12+x2\frac{\partial u}{\partial x_1} = 3x_1^2 + x_2. Substituting (2,3)(2, 3) gives ∂u∂x1(2,3)=15\frac{\partial u}{\partial x_1}(2, 3) = 15.

Taking the partial derivative with respect to x2x_2, we get ∂u∂x2=x1\frac{\partial u}{\partial x_2} = x_1. Substituting (3,4)(3, 4) gives ∂u∂x2(3,4)=3\frac{\partial u}{\partial x_2}(3, 4) = 3.

Sample 2 Explanation

Reading the Reverse Polish Notation, the expression to be differentiated is: $u = x_2 \cdot x_2 \cdot x_2 + 0 - (-10^5) \cdot (-10^5) \cdot x_2$, i.e. u=x23−1010x2u = x_2^3 - 10^{10}x_2.

Since uu actually does not contain x1x_1 and x3x_3, the results of taking partial derivatives with respect to these two variables are both 00.

Taking the partial derivative with respect to x2x_2, we get ∂u∂x2=3x22−1010\frac{\partial u}{\partial x_2} = 3x_2^2 - 10^{10}.

Subtasks

Test Point nn mm Properties of the Expression
1, 2 =1= 1 ≤100\le 100 Contains only 1 element
3, 4 ^ ^ Contains only one operator
5, 6 ≤10\le 10 Contains no more than 120 elements, and no multiplication
7, 8 ^ Contains no more than 120 elements
9, 10 ≤100\le 100 ^

Hint

In C++, you can use std::getline(std::cin, str) to read a string until the end of the line.

When computing the modulo of an integer nn by MM, if nn is negative, be careful to adjust the result into the interval [0,M)[0, M).

Translated by ChatGPT 5