When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

prompt's blog

By prompt, history, 4 years ago, In English

This is the problem statement. Here is the editorial. I understood the editorialist solution. But I want to solve this problem using Combinatorics(using Permutations and Combinations). Any help will be highly appreciated. Thank You.

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

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

Sure, the combinatorial solution is

$$$f(n) = \displaystyle3\cdot\left[\sum_{k=0}^{\displaystyle\lfloor n/2 \rfloor} 2^k\binom{n}{2k}\right] = \displaystyle 3 \cdot \left[\sum_{\displaystyle\substack{\displaystyle k=0 \\ \displaystyle k \text{ is even}}}^{\displaystyle n} \binom{n}{k} 2^{k/2}\right]$$$

(both are equivalent, just whichever summation is easier to read/understand)

Here's the implementation I submitted (and got AC):

// precompute C(n,k) for k in [0, n]
long[] ncr = new long[n + 1];
ncr[0] = 1;
for (int i = 1; i <= n; i++) {
    ncr[i] = ncr[i - 1] * (n - (i - 1)) / i;
}

long answer = 0;
for (int i = 0; i <= n; i += 2) {
    answer += ncr[i] * (1L << i / 2);
}
answer *= 3;

return answer;
  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you please your solution. Thank You

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      I'm curious, why did you want a solution with combinations rather than DP in the first place? (I think it was fun, so no complaints here, just wondering what motivated that.)

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

        I actually used to be really good maths and specifically P&C and was really creative when I was 16/17. Now my brain just don't work and I can't think anything creative when solving harder problems. I knew there is solution based on combinatorics so I wanted to know how can I come up with its solution.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
          Rev. 2   Vote: I like it -8 Vote: I do not like it

          Ah, I see. How old are you now that (as you say) "your creativity has left you"?