First of all, this problem can theoretically be done by O (Nlogn), because there is a formula that can be accelerated by polynomial multiplication on oeis.
But because the modulus is not very magnetic, so it makes nlogn difficult to write
Here's an O (N*SQRT (n)) approach
First, it's easy to see that when the size of an item is >SQRT (n), the limit of the number of items is a dummy.
This means that the size of the item >sqrt (n) is actually a full backpack
And for a complete backpack, there is another approach (refer to the division of NOIP2001 number)
Since we know that if we only use >SQRT (n) items, we use up to sqrt (n) items
You may wish to set f[i][j] to show that I have used the items of I >sqrt (n) to spell out the scheme of J
We classify the discussion:
1. At least one of these I items is sqrt (n) +1
2, this I items are >sqrt (n) +1
can easily get a recursive f[i][j]=f[i][j-i]+f[i-1][j-sqrt (n)-1]
Since a maximum of sqrt (n) items are used, the number of States is N*SQRT (n), and the Transfer is O (1)
So the time complexity of this part is O (n*sqrt (n))
We then consider using only the <=SQRT (n) condition
This is obviously a simple multi-knapsack problem.
We all know that the time complexity of multiple knapsack problems is O (number of items * Backpack size)
So the complexity of Time is O (n*sqrt (n))
After merging two cases, Time complexity O (n*sqrt (n))
Well, the upper part of the problem is almost the NOIP2001 number division.
The lower part is a bare multi-pack.
So the overall is noip difficult, and I do 51Nod when I did not want to come out
So my level is below Noip
Certificate of Completion Qaq
By the way, the final merged formula is a convolution form
51Nod Limited Backpack Counting problem report