Skip to content

Editorial for Quy hoạch động cổ điển

Quy hoạch động cổ điển

Problem code: olpicpc037c09d Group: Uncategorized Points: 2,500 pointspartial
Limits
2.0 s, 512 MB
AC rate
23.9%
Solvers
16
Read the editorial only when you are stuck, and do not copy code from it. Please respect the problem author and the editorialist: submitting an official solution before solving the problem yourself is a bannable offence.

Gọi dp[i][j] là tổng các f(l,r) với l \le r \le i, trong đó f(l,r) là số lượng cách chọn các phần tử dãy A sao cho tổng bằng j.

Ta có thể biến đổi biểu thức cần tính như sau:

\displaystyle \sum_{l=1}^{N}\sum_{r=l}^{N} f(l,r)=\sum_{l=1}^{N-1}\sum_{r=l}^{N-1} f(l,r)+\sum_{l=1}^{N}f(l,N)

Từ đó, ta có công thức QHĐ như sau:

  • dp[i][0]=i+1 với mọi 1 \le i \le N
  • dp[i][j] = dp[i-1][j] + dp[i-1][j-A_i] với mọi 1 \le i \le N, 1 \le j \le S

Độ phức tạp: O(N.S)

Comments