Mathematical Expectation
Difficulty: Hard

Let's consider a random permutation p<sub>1</sub>, p<sub>2</sub>, ..., p<sub>N</sub> of numbers 1, 2, ..., N and calculate the value F=(X<sub>2</sub>+...+X<sub>N-1</sub>)<sup>K</sup>, where X<sub>i</sub> equals 1 if one of the following two conditions holds: p<sub>i-1</sub> < p<sub>i</sub> > p<sub>i+1</sub> or p<sub>i-1</sub> > p<sub>i</sub> < p<sub>i+1</sub> and X<sub>i</sub> equals 0 otherwise. What is the expected value of F?

**Input Format:**  
The first line contains two integers K and N.

**Output Format:**  
Print the expected value of F as an irreducible fraction p / q. Follow sample input for more clarification.


**Constraints:**  
1000 <= N <= 10<sup>9</sup>  
1 <= K <= 5

**Sample input**  

    1 1000

**Sample Output**  

    1996 / 3


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:

Editorial: Mathematical Expectation - 2020 Hack February Editorial
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p><a href="https://www.hackerrank.com/contests/feb14/challenges/mathematical-expectation">Mathematical Expectation</a>  </p>

<p><strong>Difficulty</strong> : Medium-Hard <br>
<strong>Problem Setter</strong> : <a href="https://www.hackerrank.com/seyaua">Levgen Soboliev</a>  </p>

<p><strong>Approach</strong> </p>

<ol>
<li><p>Let's write simple bruteforce solution and generate all answers for small N (N&lt;=20). After that one can observe, that for each K the answer is P_K(N), where P_K is a polynom of K-th power. So, after generating small answer we can interpolate the results and get needed polynom. So, now we obtain a solution where we need to keep just the coefficients of each P_K. Do not forget that the answer can be very big, so we have to use something like BigIntegers.</p></li>
<li><p>We can look at random variable (X_1+X_2+..+X_N)^K open the parentheses and use the linearity of mathematical expectation. In this case we have to work with this variables very careful, because some of them are not independent. This way is much harder than previous, so this is just an idea :) But with this idea one can solve this problem for bigger K.</p></li>
</ol>

<p><strong>Setter's Solution</strong></p>

<pre><code>import java.io.*;
import java.util.*;
import java.math.*;

public class Solution {
    public static void main(String[] args) {
        new Solution().run();
    }

    // 1 : +2/3*n^1-4/3*n^0
    // 2 : 40*n^2-144*n+131 / 90
    // 3 : 280*n^3-1344*n^2+2063*n-1038 / 945
    // 4 : 2800*n^4-15680*n^3+28844*n^2-19288*n+4263 / 14175
    // 5 : 12320*n^5-73920*n^4+130328*n^3-29568*n^2-64150*n-5124 / 93555
    // 6 : 44844800*n^6-269068800*n^5+322081760*n^4+706113408*n^3-1344748912*n^2-329275872*n+957888513 / 510810300

    void output(BigInteger num, BigInteger denom) {
        BigInteger gcd = num.gcd(denom);
        num = num.divide(gcd);
        denom = denom.divide(gcd);
        System.out.println(num + " / " + denom);
    }

    void solve(int k, BigInteger n) {
        if (k == 1) {
            BigInteger num = BigInteger.valueOf(2).multiply(n).subtract(BigInteger.valueOf(4));
            BigInteger denom = BigInteger.valueOf(3);
            output(num, denom);
        } else if (k == 2) {
            BigInteger n2 = n.pow(2);
            BigInteger num = BigInteger.valueOf(40).multiply(n2).subtract(BigInteger.valueOf(144).multiply(n)).add(BigInteger.valueOf(131));
            BigInteger denom = BigInteger.valueOf(90);
            output(num, denom);
        } else if (k == 3) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger num = BigInteger.valueOf(280).multiply(n3).subtract(BigInteger.valueOf(1344).multiply(n2)).add(BigInteger.valueOf(2063).multiply(n)).subtract(BigInteger.valueOf(1038));
            BigInteger denom = BigInteger.valueOf(945);
            output(num, denom);
        } else if (k == 4) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger num = BigInteger.valueOf(2800).multiply(n4).subtract(BigInteger.valueOf(15680).multiply(n3)).add(BigInteger.valueOf(28844).multiply(n2)).subtract(BigInteger.valueOf(19288).multiply(n)).add(BigInteger.valueOf(4263));
            BigInteger denom = BigInteger.valueOf(14175);
            output(num, denom);
        } else if (k == 5) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger n5 = n.pow(5);

            BigInteger num = BigInteger.valueOf(12320).multiply(n5).subtract(BigInteger.valueOf(73920).multiply(n4)).add(BigInteger.valueOf(130328).multiply(n3)).subtract(BigInteger.valueOf(29568).multiply(n2)).subtract(BigInteger.valueOf(64150).multiply(n)).subtract(BigInteger.valueOf(5124));
            BigInteger denom = BigInteger.valueOf(93555);
            output(num, denom);
        } else if (k == 6) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger n5 = n.pow(5);
            BigInteger n6 = n.pow(6);

            BigInteger num = BigInteger.valueOf(44844800).multiply(n6).subtract(BigInteger.valueOf(269068800).multiply(n5)).add(BigInteger.valueOf(322081760).multiply(n4)).add(BigInteger.valueOf(706113408).multiply(n3)).subtract(BigInteger.valueOf(1344748912).multiply(n2)).subtract(BigInteger.valueOf(329275872).multiply(n)).add(BigInteger.valueOf(957888513));
            BigInteger denom = BigInteger.valueOf(510810300);
            output(num, denom);
        }
    }

    void run() {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();

        BigInteger n = in.nextBigInteger();

        solve(k, n);
    }
}
</code></pre>

Reference solution (Java):
###Java  
```java  

import java.io.*;
import java.util.*;
import java.math.*;
    
public class Solution {
    public static void main(String[] args) {
        new Solution().run();
    }
    
    // 1 : +2/3*n^1-4/3*n^0
    // 2 : 40*n^2-144*n+131 / 90
    // 3 : 280*n^3-1344*n^2+2063*n-1038 / 945
    // 4 : 2800*n^4-15680*n^3+28844*n^2-19288*n+4263 / 14175
    // 5 : 12320*n^5-73920*n^4+130328*n^3-29568*n^2-64150*n-5124 / 93555
    // 6 : 44844800*n^6-269068800*n^5+322081760*n^4+706113408*n^3-1344748912*n^2-329275872*n+957888513 / 510810300
    
    void output(BigInteger num, BigInteger denom) {
        BigInteger gcd = num.gcd(denom);
        num = num.divide(gcd);
        denom = denom.divide(gcd);
        System.out.println(num + " / " + denom);
    }
    
    void solve(int k, BigInteger n) {
        if (k == 1) {
            BigInteger num = BigInteger.valueOf(2).multiply(n).subtract(BigInteger.valueOf(4));
            BigInteger denom = BigInteger.valueOf(3);
            output(num, denom);
        } else if (k == 2) {
            BigInteger n2 = n.pow(2);
            BigInteger num = BigInteger.valueOf(40).multiply(n2).subtract(BigInteger.valueOf(144).multiply(n)).add(BigInteger.valueOf(131));
            BigInteger denom = BigInteger.valueOf(90);
            output(num, denom);
        } else if (k == 3) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger num = BigInteger.valueOf(280).multiply(n3).subtract(BigInteger.valueOf(1344).multiply(n2)).add(BigInteger.valueOf(2063).multiply(n)).subtract(BigInteger.valueOf(1038));
            BigInteger denom = BigInteger.valueOf(945);
            output(num, denom);
        } else if (k == 4) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger num = BigInteger.valueOf(2800).multiply(n4).subtract(BigInteger.valueOf(15680).multiply(n3)).add(BigInteger.valueOf(28844).multiply(n2)).subtract(BigInteger.valueOf(19288).multiply(n)).add(BigInteger.valueOf(4263));
            BigInteger denom = BigInteger.valueOf(14175);
            output(num, denom);
        } else if (k == 5) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger n5 = n.pow(5);
    
            BigInteger num = BigInteger.valueOf(12320).multiply(n5).subtract(BigInteger.valueOf(73920).multiply(n4)).add(BigInteger.valueOf(130328).multiply(n3)).subtract(BigInteger.valueOf(29568).multiply(n2)).subtract(BigInteger.valueOf(64150).multiply(n)).subtract(BigInteger.valueOf(5124));
            BigInteger denom = BigInteger.valueOf(93555);
            output(num, denom);
        } else if (k == 6) {
            BigInteger n2 = n.pow(2);
            BigInteger n3 = n.pow(3);
            BigInteger n4 = n.pow(4);
            BigInteger n5 = n.pow(5);
            BigInteger n6 = n.pow(6);
    
            BigInteger num = BigInteger.valueOf(44844800).multiply(n6).subtract(BigInteger.valueOf(269068800).multiply(n5)).add(BigInteger.valueOf(322081760).multiply(n4)).add(BigInteger.valueOf(706113408).multiply(n3)).subtract(BigInteger.valueOf(1344748912).multiply(n2)).subtract(BigInteger.valueOf(329275872).multiply(n)).add(BigInteger.valueOf(957888513));
            BigInteger denom = BigInteger.valueOf(510810300);
            output(num, denom);
        }
    }
    
    void run() {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
    
        BigInteger n = in.nextBigInteger();
    
        solve(k, n);
    }
}
```