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

Автор coderdhanraj, 4 года назад, По-английски

Hey Everyone,

Recently, I had my Salesforces Coding Test and I got 3 problems in which I solved 2 problems fully and solved the following problem partially with $$$O(n * m * m)$$$ using DP. Can anyone tell me the $$$O(n)$$$ approach for this problem?

Problem : Bob and Problems

Bob and Alice are preparing $$$n$$$ problems for Hackerrank. [n-problem together can be considered as 1 problem-set]

The hardness level of the problem $$$i$$$ will be an integer $$$a_i$$$, where $$$a_i \ge 0.$$$

The hardness of the problems in a given problem-set must satisfy $$$a_i + a_{i + 1} \lt m$$$, and $$$a_1 + a_n \lt m$$$, where $$$m$$$ is a fixed integer.

Bob wants to know how many different problem-set are possible. And since this number can be huge, he wants Alice to find it under modulo $$$998244353$$$.

Note :

  1. Two problem-sets of difficulty $$$a$$$ and $$$b$$$ are different only if there is an integer $$$j$$$ ($$$1 \le i \le n$$$) satisfying $$$a_i \neq a_j$$$.

  2. You can choose $$$a_i$$$ as any integer greater than or equal to $$$0$$$, as long as it satisfies the constraints on line-3.

Constrains :

$$$2 \le n \le 10^5$$$, $$$1 \le m \le 10^9$$$

Sample Test Case :

Input : 3 2

Output : 4

Explanation :

The valid different problem-sets are [0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0].

[1, 0, 1] is invalid since $$$a_1 + a_n \ge m$$$ here.

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

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

I think number of valid different permutations should be = (m*(m+1)*(m+2)*...*(m+n-1))/(n*(n-1))

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

I proved the correctness of the following formula for $$$n = 2$$$ and $$$3$$$.

$$$f(n,m) = \binom{m+n-1}{n}$$$.

You may try to prove it by induction for larger values of $$$n$$$. It is possible to compute $$$f(n,m)$$$ in $$$O(n)$$$ time-complexity using $$$n-1$$$ multiplication operations of integer numbers between $$$m$$$ and $$$m+n-1$$$ inclusive, and dividing the result by $$$n!$$$ modulo $$$998244353$$$.

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

    no no no

    You don't calculate newtons binomial like that :)

    you dont divide by n!, you multiply by inversions of every number between [2 ... n], so

    inv(2) * inv(3) * ... * inv(n) (of course keeping everything in check using modulo)

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

      Ok, thanks.

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

      or it would rather be smarter to preprocess the inversions for the factorials at the same loop you do the factorials

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

      I usually use the two loops in the constructor of the following modular combinatorics class template to compute the required inverse of $$$n!$$$ using the modular inversion only one time.

      modular combinatorics class template
»
4 года назад, скрыть # |
 
Проголосовать: нравится +38 Проголосовать: не нравится

This problem is a blatant copy of 1580F - Problems for Codeforces. It's also very difficult and the previous commenters' answers are wrong.