SPyofgame's blog

By SPyofgame, history, 4 years ago, In English

About the problem

The problem is to calculate the number of such subsequence $$${a_1, a_2, \dots a_n}$$$ that ($$$a_1 + a_2 + \dots + a_k = n$$$) where ($$$k \geq 2$$$) and ($$$a_i \in {1, 2, \dots, n}$$$)

It is the sequence OEIS A111133



My approach for small n

Lets $$$magic(left, last)$$$ is the number of valid subsequences whose sum equal $$$left$$$ which next selected element is such $$$next$$$ in range $$$(last, left]$$$ ($$$next$$$ is strictly greater then last selected number $$$last$$$ and not greater than current sum $$$left$$$). The recursive stop when $$$left = 0$$$ then we found one valid subsequence

Recursive dp - O(n^3) - small n


My approach for bigger n

Lets $$$magic(sum, cur)$$$ is the number of valid subsequences whose selected sum is $$$sum$$$ and current selecting element is $$$cur$$$

  • $$$cur$$$ is init as $$$1$$$ (smallest element) and recursive stop when it greater than $$$n$$$ (largest element)

  • $$$sum$$$ is in range $$$[0, n]$$$ when it equal $$$n$$$ then we found 1 valid subsequence so we return $$$1$$$, else if it greater than $$$n$$$ we stop the recursive

The complexity is still $$$O(n^3)$$$ which $$$O(n^2)$$$ calculation and $$$O(n)$$$ for bignum implementation

Recursive dp - O(n^3) - bignum result
Iterative dp - O(n^3) - bignum result


My question

  • Can I solve the problem in $$$O(n^2)$$$ or in $$$O(n^2 \times polylog(n))$$$

  • Can I find the n_th element faster than $$$O(n^3)$$$

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
4 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

You could use the generating function given in oeis, and use divide and conquer multiplication on each individual polynomial, and while that restrict the highest power of each part upto N, because we don't require more than N.

At certain depth from bottom prefer manual multiplication of polynomial because powers are sparse,after that depth use fft to speed because after some depth there will be more powers to deal with , even if you use fft for all depths .

it will be no more N^2 log, and coefficient of X^N will be your answer.

EDIT :: here is simple explanation of the above logic, Also a N^2 dp approach, link

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can you give me a link of this problem?