Codeforces and Polygon may be unavailable from May 23, 4:00 (UTC) to May 23, 8:00 (UTC) due to technical maintenance. ×

Misa-Misa's blog

By Misa-Misa, 8 months ago, In English

Problem

You are given positive integers N and L. Count all sequences of positive integers with the following properties:
- The sequence is strictly increasing.
- The length of the sequence is L.
- No two consecutive elements share the same parity.

Return the number of these sequences, modulo 1e9 + 7.

Constraints

N <= 1e5 and L <= N.
elements in sequence can be from 1 to N.

What I have been able to do upto now

I broke it into two parts.
- Part 1 : o e o e o e ...
- Part 2 : e o e o e o ...
I have thought of dp.

Then Lets solve for o e o e .., second part will be similar.

dp[i][j] : Number of ways to fill upto index i such that number at index i is j. If i is odd then j must be odd.

dp[i][j] = dp[i-2][j-2] * 1 + dp[i-2][j-4] * 2 + dp[i-2][j-6] * 3 ... -- eq1

This is O(N*N*N) Solution.

Then i further observed that

dp[i][j-2] = dp[i-2][j-4] * 1 + dp[i-2][j-6] * 2 + dp[i-1][j-8] * 3 ... --eq2

Then eq1 - eq2 gives me,

dp[i][j] = dp[i][j-2] + dp[i-2][j-2] + dp[i-2][j-4] + dp[i-2][j-6] + dp[i-2][j-8] ...

this can now be done in O(N*N), but will surely give TLE in above constraints.

So how to proceed further?

Are there any optimization / tricks in this dp.

Or I should think something else which is not dp.

Don't wanna know the solution, just tell if there is some general concept/trick that i need to know, or i just need to think more on it.

Thanks.

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

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Misa-Misa (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Misa-Misa (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Misa-Misa (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it +22 Vote: I do not like it

What does $$$N$$$ represent in the problem? The upper bound on $$$a_L$$$?

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Misa-Misa (previous revision, new revision, compare).

»
8 months ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

Try to interpret the problem in combinatorical terms. Formally, think of this task instead of what is literally written in the statements.

  • Count the number of arrays $$$b$$$ of length $$$L$$$, such that...
  • The sum of elements is $$$N$$$.
  • Only the first element of $$$b$$$ can be even. The rest can be only odd.
The part below contain spoilers
»
8 months ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

Any sequence that fulfills the said conditions can be constructed like this:

Start with a continuous segment of the numbers 1,2,...N, like [i, i+L-1].

Now consider each suffix of this sequence, you can add any even amount to it as long as the value of the largest element (the last element) doesn't exceed N.

To make it easier, try thinking of the remaining value (N — (i+L-1)) as AddSuffix(i, +2) operations up to (N-(i+L-1))/2 times. Now you only have to think about how to distribute some of those operations on the suffixes of the sequence.

In order to consider not using all those operations, you can add an additional element at the end of the sequence, which will not be included in the final sequence.

Time complexity O(N).

in my code, I use n=L and mx=N.

Code
  • »
    »
    8 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    I think this can be optimized to only 2 scenarios:

    1,2,...n

    and

    2,3....n+1

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it
    code
»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

(I can be wrong)

Between each two consequtive elemets we need to add 1 exactly one time and add 2 some number of times (maybe 0).

Spoiler
Spoiler
Spoiler
Spoiler