# LDRBRD Editorial, ONCO2020

**URL:** <https://discuss.codechef.com/t/ldrbrd-editorial-onco2020/52440>\
**Category:** editorial\
**Tags:** combinatorics, dynamic-programming, editorial, maths, matrix-exponentiation, onco2020\
**Created:** [February 8, 2020, 8:13pm UTC](https://discuss.codechef.com/t/ldrbrd-editorial-onco2020/52440 "2020-02-08T20:13:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![puneetrai04](https://discuss.codechef.com/user_avatar/discuss.codechef.com/puneetrai04/32/6935_2.png) [@puneetrai04](https://discuss.codechef.com/u/puneetrai04)\
**Post date:** [February 8, 2020, 8:13pm UTC](https://discuss.codechef.com/t/ldrbrd-editorial-onco2020/52440/1 "2020-02-08T20:13:40Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/LDRBRD)  
[Contest](https://www.codechef.com/ONCO2020/problems/LDRBRD)

**Setter:** [Puneet Rai](https://www.codechef.com/users/puneetrai04)

# DIFFICULTY:

Medium-Hard

# PREREQUISITES:

Dynamic Programming, Matrix Exponentiation, Maths.

# PROBLEM:

Considering there are N people participating and there are M problems in the contest. What would be the expected number of problems solved by the person at rank K on the leaderboard?

If the expected value is {P/Q} you must print the value {P\*Q^{-1} \% MOD}, where {MOD=10^9 + 7}.

# EXPLANATION

Consider the leaderboard as a matrix of size {N\*M}.

We define {f(n,m)} as the number of total configurations that are possible if there are n participants and m problems.

{f(n,m) = \sum\_{i=0}^{m} {m \choose x}\*f(n-1,i)}

This can be calculated in {O(m^3log\_2(n))} using matrix exponentiation :-

{\begin{bmatrix} f\_{n-1}(0) & f\_{n-1}(1) & . & . & . & f\_{n-1}(m) \end{bmatrix} \* M = \begin{bmatrix} f\_n(0) & f\_n(1) & . & . & . & f\_n(m) \end{bmatrix}}  
where, M is {(m+1)\*(m+1)} matrix, such that -

${M = \begin{bmatrix}{m \choose 0} & {m \choose 0} & . & . & {m \choose 0} \  
0 & {m \choose 1} & . & . & {m \choose 1} \  
0 & 0 & . & . & {m \choose 2} \  
. & . & . & . & . \  
. & . & . & . & .\  
0 &0 & . & . & {m \choose m} \ \end{bmatrix}}$

Therefore {F\_n = F\_0 \* M^n} and {F\_n(i)} is the number of configurations such no one solved more than i problems.

and {F\_0 = \begin{bmatrix} 1 & 1 & . & . & . & 1 \end{bmatrix}}

We define {g(n,m,k)} as the number of total configurations that are possible if there are n participants and m problems and the last participant must solve at least k problems.

{g(n , m , k) = F\_{0}^{'} \* M^n}, where

{F\_{0}^{'} = \begin{bmatrix} 0 & 0 & . & 1 & 1 & . & 1 \end{bmatrix}} such that for all {i \geq k}, {F\_{0}^{'} (i)= 1}.

We define {h(n , m , k)} as the number of total configurations that are possible if there are n participants and m problems and the first participant must solve at most k problems.

{F\_n = F\_{0} \* M^n}, and -

{h(n , m , k) = F\_n(k)}

Now, the answer to the problem is the expected value of the number of problems solved by person k, which can be calculated for each i where i is the number of problems solved by a person at rank k as -

{answer = \sum\_{i=0}^m p(k , i) \* i }

where {p(i) = } the probability of solving i problems by the person at rank k.

and {p(k , i) = {m \choose i} \* g(k-1 , m , i) \* h(n-k, m, i) / f(n , m)}

i.e. product of all the possibilities of choosing i problems for rank k , number of ways such that the top {k-1} people solve at least i problems and number of ways such that the bottom {n-k} people solve at most i problems.

Therefore the final answer will be,

{answer = f(n , m)^{-1} \* \sum\_{i=0}^m {m \choose i} \* g(k-1 , m , i) \* h(n-k, m, i) \* i}

Note - g and h can be calculated in {O(n^2)} by precomputing the matrix M for each of them separately.

So, the total time complexity will be {O(M^3 log\_2(N) \* T)}.

Time Complexity: {O(T\*M^3\*log\_2(N)})  
Space Complexity: O(M^3)  
Problem Setter Code: [Solution](https://www.codechef.com/viewsolution/29436492)
