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

piyushpandey's blog

By piyushpandey, history, 3 years ago, In English

Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)Problem C Problem Link

I read the editorial it used brute force approach

Is it possible to do this question using math? (without checking for 10^4 possible passwords)

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Yes you can solve this problem with combinatorics.

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

If the pins were of unique elements, you would solve it with basic combinatorics. But since that's not the case, you should still iterate over the elements to subtract those overlaps, considering many cases.

Still, that's my opinion, I am very interested if some, not complicated formula exists for this.

»
3 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

you can use combinatorics:

  • if an element is fixed ('o') you can use polynomial: $$$exp(x) - 1 = \frac{x}{1!} + \frac{x^2}{2!} + ... $$$

  • in other case ('?'): $$$exp(x) = 1 + \frac{x}{1!} + \frac{x^2}{2!} + ... $$$

The answer consists first of multiplying all these polynomials and answering the coefficient 4 multiplied by 4!

Using the fact that $$$exp(x) ^ c = exp(c * x)$$$ you have 6 cases for each number of characters 'o'.

code

answer $$$= \sum\limits_{i=0}^b (-1)^i \binom{b}{i} (a + b - i)^4$$$

where a = #'?' and b = #'o'.

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

    In general you need to calculate the coefficients of $$$(x - 1)^k$$$, which can be achieved in $$$O(k)$$$.