Polynomials

시간 제한3초메모리 제한1024 MB

요약
왼쪽의 N개 다항식에서 시작해 미분과 적분을 최소 횟수로 적용하여 오른쪽의 M개 다항식 각각을 만드는 최소 행동 수를 구한다.
난이도

어려움10점 중 8점

유형
해시맵, 수학, 문자열, 구현
정답자
아직 제출이 없습니다

문제

On the left side of the board, there are NN polynomials, and on the right side, there are MM polynomials. Your task is to construct each of the MM polynomials from the right side of the board with the minimum number of actions.

To construct a polynomial from the right side, first choose any of the NN polynomials from the left side. The following transformations can be applied to the chosen polynomial:

  • Differentiation: a polynomial is replaced by its derivative.
  • Integration: a polynomial is replaced by its antiderivative with an arbitrary integration constant.

Transformations can be applied in arbitrary order any number of times, however, one application counts as one action. You can use no transformations at all if a polynomial from the right side matches some polynomial from the left side.

입력

The first line of the input file contains two integers: NN and MM (1≤N,M≤1051\leq N,M\leq 10^5).

Each of the following N+MN+M lines describes the polynomials, one polynomial per line. The first NN polynomials are polynomials from the left side of the board, the rest are from the right side of the board.

The description of the polynomial a_0+a_1x+⋯+a_KxKa\_0 + a\_1 x + \dots + a\_K x^K begins with a nonnegative integer KK --- its degree. Next come integers a_0,a_1,…,a_Ka\_0, a\_1, \dots, a\_K, which are the coefficients of the polynomial (−109≤a_i≤109-10^9\leq a\_i \leq 10^9). Herewith, a_K≠0a\_K\neq 0.

It is guaranteed that the sum of degrees of all polynomials on the left side of the board is not greater than 10510^5. It is the same for the polynomials on the right side of the board.

출력

For each polynomial from the right side, print the minimum number of actions necessary for its construction. Print your answers one per line in the same order as the order in which the polynomials are listed in the input file.

힌트

In the second example there are two polynomials on the left side of the board: p_1(x)=1+x+x2p\_1(x)=1+x+x^2 and p_2(x)=7+6x+2x2p\_2(x)=7+6x+2x^2. We need to obtain the polynomial q(x)=7+6x+x2q(x)=7+6x+x^2. In order to do that we apply differentiation to p_1p\_1 twice obtaining p_1′(x)=1+2xp\_1'(x)=1+2x first, and p_1"(x)=2p\_1"(x)=2 next. Now, let's integrate p_1"(x)p\_1"(x) with 66 as the integration constant producing 6+2x6+2x. We integrate the result once again with 77 as the integration constant to get 7+6x+x27+6x+x^2. We used 44 actions in total.

예제2

  1. 예제 1

    입력
    2 1
    2 1 1 1
    2 7 6 2
    2 7 6 1
    
    예상 출력
    4
    
  2. 예제 2

    입력
    2 1
    2 1 1 1
    0 1
    1 1 1
    
    예상 출력
    1