Блог пользователя __fn__

Автор __fn__, история, 3 года назад, По-английски

You are given an integer $$$n \leq 10^9$$$. Your task is to compute the number of ways $$$n$$$ can be expressed as the sum of even numbers. Since the answer could be very large, compute it modulo $$$10^9 + 7$$$.

Time limit : 1s

Sample : $$$n = 8$$$

$$$8 = 6 + 2$$$

$$$8 = 4 + 4$$$

$$$8 = 4 + 2 + 2$$$

$$$8 = 2 + 2 + 2 + 2$$$

Therefore for $$$n = 8$$$ the answer would be $$$4$$$

Do anyone know how to solve this problem? Comment on the solution

UPDATE Thanks to Wielomian and estoy-re-sebado for sharing some ways to solve this problem

Solution
  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is there any link to the problem?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think this is DP

»
3 года назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

If $$$n$$$ is odd, the answer is $$$0$$$. Else you can divide $$$n$$$ by $$$2$$$, so we can rewrite the problem as:

Compute the number of ways $$$n$$$ can be expressed as the sum of positive numbers which are strictly less than $$$n$$$.

This is a classical problem. You could solve it with GF in $$$\mathcal O(n \log n)$$$. I don't know if a better solution exists.

  • »
    »
    3 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    What’s GF? And is a $$$O(nlogn)$$$ solution possible to pass when n is $$$5\times 10^8$$$?

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    After division by $$$2$$$, what remains from the right-hand side are exactly the partitions, so the answer is $$$p(n / 2) - 1$$$. The Wikipedia article even shows the same example as in the problem statement, after the division by $$$2$$$.

    The computation of $$$p(n)$$$ can be done roughly in $$$O(\sqrt{n})$$$ complexity, by a somehow convoluted combinatorial argument, see this stackexchange thread.

    • »
      »
      »
      3 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Can you explain why this problem is equal to the partition one? I generated answers up to $$$150$$$ for my problem and I was able to see that the terms of those two series are different just by 1 but I still don't know why.

      • »
        »
        »
        »
        3 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        A partition is pretty much what the problem asks, by definition. From Wikipedia:

        In number theory and combinatorics, a partition of a non-negative integer n, also called an integer partition, is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition.

        The one leftover is when you express $$$n$$$ as $$$n$$$ and not as a sum, which the definition of partition allows but the problem doesn't.

        • »
          »
          »
          »
          »
          3 года назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Ok but in standard when doing a partition we can use all numbers that are less than the number that is being partitioned. But in this problem, we are only allowed to use even number so how do you do the transition from general case to this restriction.

    • »
      »
      »
      3 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Do you also think that the algorithm which run in $$$O(\sqrt{n})$$$ can be implemented for cp use?

      • »
        »
        »
        »
        3 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        I don't think that this algorithm is usable for any problem with rating below 3000. It's rather a technical, mathematical curiosity. If you want to know something about computing the partition number function, the usual way is a dp by direct application of Euler's Pentagonal Number Theorem. This allows to compute all $$$p(n)$$$s up to some $$$N$$$ in time $$$O(N \sqrt{N})$$$ (as pentagonal numers grow quadratically).

        • »
          »
          »
          »
          »
          3 года назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Ok. I finally managed to implement the one with $$$dp$$$ using $$$\sum{p_{k}(n)}$$$ in $$$O(N^2)$$$ yesterday. I have just checked about the pentagonal number and we can achieve that time complexity of $$$O(N\sqrt{N})$$$ you have mentioned. $$$Thanks !!$$$

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Maybe there's something that can be done by first writing it as a sum of n/2 2s, then computing the ways you can group them together? Not sure though

»
3 года назад, скрыть # |
← Rev. 7  
Проголосовать: нравится -16 Проголосовать: не нравится

Basically the answers form a fibonacci sequence ,, for: n= 6 ans=2, n=8 ans=4, n=10 ans=6, n=12 ans=10, n=14 ans=16, n=16 ans=26,

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится +10 Проголосовать: не нравится

    They don't follow the Fibonacci sequence, because why would they? For n = 14 the answer is 14 not 16:

    1. 14 = 12 + 2
    2. 14 = 10 + 4
    3. 14 = 10 + 2 + 2
    4. 14 = 8 + 6
    5. 14 = 8 + 4 + 2
    6. 14 = 8 + 2 + 2 + 2
    7. 14 = 6 + 6 + 2
    8. 14 = 6 + 4 + 4
    9. 14 = 6 + 4 + 2 + 2
    10. 14 = 6 + 2 + 2 + 2
    11. 14 = 4 + 4 + 4 + 2
    12. 14 = 4 + 4 + 2 + 2 + 2
    13. 14 = 4 + 2 + 2 + 2 + 2 + 2
    14. 14 = 2 + 2 + 2 + 2 + 2 + 2 + 2
»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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