Bessie knows a number x+0.5x+0.5 where xx is some integer between 00 to N,N, inclusive (1≤N≤50001≤N≤5000).
Elsie is trying to guess this number. She can ask questions of the form "is ii high or low?" for some integer ii between 11 and N,N, inclusive. Bessie responds by saying "HI!" if ii is greater than x+0.5x+0.5, or "LO!" if ii is less than x+0.5x+0.5.
Elsie comes up with the following strategy for guessing Bessie's number. Before making any guesses, she creates a list of NN numbers, where every number from 11 to NN occurs exactly once (in other words, the list is a permutation of size NN.) Then, she goes through the list, guessing numbers that appear in the list in order. However, Elsie skips any unnecessary guesses. That is, if Elsie is about to guess some number ii and Elsie previously guessed some j<ij<i such that Bessie responded with "HI!," Elsie will not guess ii and will move on to the next number in the list. Similarly, if she is about to guess some number ii and she previously guessed some j>ij>i such that Bessie responded with "LO!," Elsie will not guess ii and will move on to the next number in the list. It can be proven that using this strategy, Elsie always uniquely determines xx regardless of the permutation she creates.
If we concatenate all of Bessie's responses of either "HI" or "LO" into a single string S,S, the number of times Bessie says "HILO" is the number of length 44 substrings of SS that are equal to "HILO."
Bessie knows that Elsie will use this strategy and has already chosen the value of xx, but she does not know what permutation Elsie will use. Your goal is to compute the sum of the number of times Bessie says "HILO" over all permutations that Elsie could possibly choose, modulo 109+7109+7.
The only line of input contains NN and xx.
The total number of HILOs modulo 109+7109+7.
4 2
17
In this test case, Bessie's number is 2.52.5.
For example, if Elsie's permutation is (4,1,3,2)(4,1,3,2), then Bessie will say "HILOHILO," for a total of two "HILO"s. As another example, if Elsie's permutation is (3,1,2,4)(3,1,2,4), then Bessie will say "HILOLO," for a total of one "HILO."
60 10
508859913
Make sure to output the sum modulo 109+7109+7.
Problem credits: Richard Qi
[/hide]
(Analysis by Richard Qi, Benjamin Qi)
There are two ways to approach this problem. One uses dynamic programming, while the other uses linearity of expectation.
1. DP Solution
Suppose that we have already constructed some prefix of the permutation, and there are jj remaining non-redundant values below Bessie's number and kk remaining non-redundant values above Bessie's number (we define non-redundant numbers as numbers that Elsie could guess that would give her more information). Also, consider the two cases where Bessie has most recently said either "HI" or "LO". Let b=0b=0 be the case where Bessie has most recently said "LO" and let b=1b=1 be the case where Bessie has most recently said "HI". Then, the expected number of "HILO"s for the original problem is just the case where j=x,k=N−x,b=0j=x,k=N−x,b=0.
Thus, it is sufficient to count the expected number of "HILO"s for each j≤x,k≤N−x,j≤x,k≤N−x, and b=0b=0 or b=1b=1. Call this quantity dp[b][j][k]dp[b][j][k]. Now, note that all redundant values do not affect the expected number of "HILOs", so we ignore all of them. Then, there are j+kj+k possible values for the next number in the permutation, each of which occurs with probability 1j+k1j+k.
We will first consider the case where b=0b=0. Suppose that the next value in the permutation is less than x+0.5,x+0.5, of which there are jj possibilities. Denote these values as v0,v1,v2,⋯vj−1v0,v1,v2,⋯vj−1 such that x+0.5>v0>v1>v2⋯>vj−1x+0.5>v0>v1>v2⋯>vj−1.
Suppose the next value in the permutation is vj2vj2 for some 0≤j2<j0≤j2<j. Then, Bessie says "LO" (because vj2<x+0.5vj2<x+0.5) and there are j2j2 remaining nonredundant values below Bessie's number. The kk initial nonredundant values above Bessie's number are still nonredundant, so the expected number of HILOs in the remaining sequence of values is dp[0][j2][k]dp[0][j2][k].
Similarly, if we consider the values greater than x+0.5,x+0.5, which we label x+0.5<u0<u1<u2⋯<uk−1,x+0.5<u0<u1<u2⋯<uk−1, if we choose one of these values uk2uk2, Bessie says "HI" and there are k2k2 remaining nonredundant values above Bessie's number, so the expected number of HILOs in the remaining sequence of values is dp[1][j][k2]dp[1][j][k2].
So, we have the recurrence dp[0][j][k]=∑j2<jdp[0][j2][k]+∑k2<kdp[1][j][k2]j+kdp[0][j][k]=∑j2<jdp[0][j2][k]+∑k2<kdp[1][j][k2]j+k.
For computing dp[1][j][k],dp[1][j][k], we can apply the same technique. However, since Bessie has just said "HI", if we now choose a value vj2<x+0.5,vj2<x+0.5, Bessie will then say "LO" and the number of "HILO"s increases by one. We choose a value less than x+0.5x+0.5 with probability jj+k,jj+k, so the expected number of "HILO"s increases by jj+k.jj+k.
So, we have the recurrence dp[1][j][k]=∑j2<jdp[0][j2][k]+∑k2<kdp[1][j][k2]j+k+jj+k.dp[1][j][k]=∑j2<jdp[0][j2][k]+∑k2<kdp[1][j][k2]j+k+jj+k.
Now, we can compute every value of dp[b][j][k]dp[b][j][k] by iterating over all j2<j,k2<kj2<j,k2<k and using the above recurrences. Since there are N2N2 dp states and we sum over NN smaller states, this leads to a time complexity of O(N3),O(N3), which is enough for the first two subtasks.
Danny Mittal's code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ExpectedHILOCubic { public static final long MOD = 1000000007; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int x = Integer.parseInt(tokenizer.nextToken()); int y = n - x; long[] inverse = new long[n + 1]; for (int k = 1; k <= n; k++) { inverse[k] = inverse(k); } long[][][] dp = new long[2][x + 1][y + 1]; for (int j = 0; j <= x; j++) { for (int k = 0; k <= y; k++) { for (int j2 = 0; j2 < j; j2++) { dp[0][j][k] += dp[0][j2][k]; } for (int k2 = 0; k2 < k; k2++) { dp[0][j][k] += dp[1][j][k2]; } dp[0][j][k] %= MOD; dp[0][j][k] *= inverse[j + k]; dp[0][j][k] %= MOD; dp[1][j][k] = (dp[0][j][k] + (inverse[j + k] * ((long) j))) % MOD; } } long answer = dp[0][x][y]; for (long k = 1; k <= n; k++) { answer *= k; answer %= MOD; } System.out.println(answer); } public static long inverse(long x) { long s = x; long res = 1; long exponent = MOD - 2; while (exponent != 0L) { if ((exponent & 1L) == 1L) { res *= s; res %= MOD; } exponent /= 2L; s *= s; s %= MOD; } return res; } }
To speed this up, note that the bottleneck of the runtime comes from computing the terms like ∑j2<jdp[0][j2][k].∑j2<jdp[0][j2][k]. Now, suppose we are computing the value dp[0][j][k]dp[0][j][k] and want to find the value ∑j2<jdp[0][j2][k]∑j2<jdp[0][j2][k] without using a loop. To do this, note that we have already computed the value of dp[0][j−1][k],dp[0][j−1][k], which required the value of ∑j2<j−1dp[0][j2][k].∑j2<j−1dp[0][j2][k]. Since we have already computed ∑j2<j−1dp[0][j2][k],∑j2<j−1dp[0][j2][k], we can use the fact that ∑j2<jdp[0][j2][k]=dp[0][j−1][k]+∑j2<j−1dp[0][j2][k],∑j2<jdp[0][j2][k]=dp[0][j−1][k]+∑j2<j−1dp[0][j2][k], which takes O(1)O(1) time instead of O(N)O(N) time to compute.
Since we can now compute the N2N2 terms of the DP in O(1)O(1) time each, our overall time complexity is now O(N2).O(N2).
Danny Mittal's code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ExpectedHILO { public static final long MOD = 1000000007; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int x = Integer.parseInt(tokenizer.nextToken()); int y = n - x; long[] inverse = new long[n + 1]; for (int k = 1; k <= n; k++) { inverse[k] = inverse(k); } long[][][] dp = new long[2][x + 1][y + 1]; long[] sums1 = new long[x + 1]; long[] sums2 = new long[y + 1]; for (int j = 0; j <= x; j++) { for (int k = 0; k <= y; k++) { dp[0][j][k] = (inverse[j + k] * (sums1[j] + sums2[k])) % MOD; dp[1][j][k] = (inverse[j + k] * (sums1[j] + sums2[k] + ((long) j))) % MOD; sums1[j] += dp[1][j][k]; sums1[j] %= MOD; sums2[k] += dp[0][j][k]; sums2[k] %= MOD; } } long answer = dp[0][x][y]; for (long k = 1; k <= n; k++) { answer *= k; answer %= MOD; } System.out.println(answer); } public static long inverse(long x) { long s = x; long res = 1; long exponent = MOD - 2; while (exponent != 0L) { if ((exponent & 1L) == 1L) { res *= s; res %= MOD; } exponent /= 2L; s *= s; s %= MOD; } return res; } }
2. Linearity of Expectation Solution
We will distinguish between two types of HILOs and count both of them. Recall the definition of the string SS in the problem statement. Consider a "HILO," which occurs at position ii in string SS. We call this "HILO" "special" if there is no "LO" before position ii; otherwise, we call it a "normal" HILO. For a normal HILO, denote the rightmost "LO" before position ii as the "corresponding" LO to this normal HILO.
First, we will count the number of "normal" HILOs.
Instead of summing the number of HILOs over all permutations, we will count the number of permutations over every possible HILO.
Formally, for all a,b,c,pa,pb,pca,b,c,pa,pb,pc such that 1≤a,b,c,pa,pb,pc≤N1≤a,b,c,pa,pb,pc≤N and c<a<b,c<a<b, we will count the number of permutations such that the following conditions hold:
1. The aath value in the permutation is pa,pa, the bbth value in the permutation is pbpb, and the ccth value in the permutation is pcpc.
2. Bessie says "HI" at position aa, "LO" at position bb, and "LO" at position c.c.
3. The "HI" at position aa and the "LO" at position bb together form a "HILO" with corresponding LO at position cc.
To enforce these conditions, it is necessary and sufficient that the following conditions hold:
4. pc<pb<x+0.5<papc<pb<x+0.5<pa
5. For all values vv in the permutation before bb (other than papa and pcpc), either pa<vpa<v or v<pc.v<pc.
Condition 44 ensures that the values are in the right order, while condition 55 ensures that the "HI" at position aa and the "LO" at position bb together form a "HILO" with corresponding LO at position cc.
Now, it remains to count the number of permutations such that conditions 44 and 55 hold. There are N−pa+pc−1N−pa+pc−1 possibilities for the remaining values in the permutation before position b,b, and there are b−3b−3 values that need to be chosen, for a total of (N−pa+pc−1)!(N−pa+pc−1−(b−3))!(N−pa+pc−1)!(N−pa+pc−1−(b−3))! ways to choose these values. After these values have been chosen, the remaining values in the permutation after position bb can be ordered arbitrarily, of which there are N−bN−b left, for a total of (N−b)!(N−b)! ways.
So, the desired sum is ∑a,b,c,pa,pb,pc∣1≤c<a<b≤N,1≤pc<pb<x+0.5<pa≤N(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!∑a,b,c,pa,pb,pc∣1≤c<a<b≤N,1≤pc<pb<x+0.5<pa≤N(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!. If we sum this naively, we get a O(N6)O(N6) solution, which is enough for 1010 test cases.
Note that pb,a,cpb,a,c do not appear in the summand, so we can easily rewrite the sum as ∑b,pc,pa∣1≤b≤N,pc<x+0.5<pa(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!⋅(x−pc)⋅(b−12).∑b,pc,pa∣1≤b≤N,pc<x+0.5<pa(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!⋅(x−pc)⋅(b−12). If we sum this naively, we get a O(N3)O(N3) solution, which is enough for 1818 test cases.
Now, to optimize this further, suppose b,pcb,pc are fixed values. Then, the above summand can be expressed in the form ∑npa=x+1C1(C2−pa)!(C3−pa)!∑pa=x+1nC1(C2−pa)!(C3−pa)!, which is also of the form ∑C5pa=C4C6(C7+paC8)∑pa=C4C5C6(C7+paC8) for constants CiCi in terms of b,pc.b,pc. Using the Hockey Stick Identity, we have that ∑C5pa=C4C6(C7+paC8)=C6(C7+C5+1C8+1)−C6(C7+C4C8+1),∑pa=C4C5C6(C7+paC8)=C6(C7+C5+1C8+1)−C6(C7+C4C8+1), which can be computed in O(1)O(1) time for fixed b,pc.b,pc.
Alternatively, we can iterate over the value of pc−pa,pc−pa, which does not require Hockey Stick.
Thus, for each of the N2N2 possible pairs b,pc,b,pc, we can find the value of the summand in O(1)O(1) time, for a total of O(N2)O(N2) time.
Counting special HILOs can be done similarly, but we only need to iterate over values of a,b,pa,pb,a,b,pa,pb, and it turns out that only papa and bb appear in the summand, so the sum can easily be optimized to O(N2)O(N2) without using Hockey Stick. Using an application of Hockey Stick, we can reduce counting special HILOs to an O(N)O(N) calculation, but this is not necessary to solve the problem.
Richard's N6N6 code:
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9+7; /** * Description: Modular arithmetic. * Source: KACTL * Verification: https://open.kattis.com/problems/modulararithmetic * Usage: mi a = MOD+5; cout << (int)inv(a); // 400000003 */ struct mi { int v; explicit operator int() const { return v; } mi():v(0) {} mi(ll _v):v(int(_v%MOD)) { v += (v<0)*MOD; } }; mi& operator+=(mi& a, mi b) { if ((a.v += b.v) >= MOD) a.v -= MOD; return a; } mi& operator-=(mi& a, mi b) { if ((a.v -= b.v) < 0) a.v += MOD; return a; } mi operator+(mi a, mi b) { return a += b; } mi operator-(mi a, mi b) { return a -= b; } mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); } mi& operator*=(mi& a, mi b) { return a = a*b; } mi pow(mi a, ll p) { assert(p >= 0); // won't work for negative p return p==0?1:pow(a*a,p/2)*(p&1?a:1); } mi inv(mi a) { assert(a.v != 0); return pow(a,MOD-2); } mi operator/(mi a, mi b) { return a*inv(b); } bool operator==(mi a, mi b) { return a.v == b.v; } using vmi = vector<mi>; /** * Description: pre-compute factorial mod inverses, * assumes $MOD$ is prime and $SZ < MOD$. * Time: O(SZ) * Source: KACTL * Verification: https://dmoj.ca/problem/tle17c4p5 */ vmi invs, fac, ifac; void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; for(int i = 2; i < SZ; i++) invs[i] = mi(-(ll)MOD/i*(int)invs[MOD%i]); for(int i = 1; i < SZ; i++) fac[i] = fac[i-1]*i, ifac[i] = ifac[i-1]*invs[i]; } mi comb(int a, int b) { if (a < b || b < 0) return 0; return fac[a]*ifac[b]*ifac[a-b]; } mi FAC(int a){ if(a < 0) return mi(0); return fac[a]; } mi IFAC(int a){ if(a < 0) return mi(0); return ifac[a]; } int main() { genFac(20005); int N, X; cin >> N >> X; mi ans = mi(0); for(int c = 1; c <= N; c++){ for(int a = c+1; a <= N; a++){ for(int b = a+1; b <= N; b++){ for(int p_c = 1; p_c <= N; p_c++){ for(int p_b = p_c+1; p_b <= N; p_b++){ if(p_b > X) continue; for(int p_a = X+1; p_a <= N; p_a++){ ans+=FAC(N-p_a+p_c-1)*FAC(N-b)*IFAC(N-p_a+p_c-1-b+3); } } } } } } for(int a = 1; a <= N; a++){ for(int b = a+1; b <= N; b++){ for(int p_b = 1; p_b <= X; p_b++){ for(int p_a = X+1; p_a <= N; p_a++){ ans+=FAC(N-p_a)*FAC(N-b)*IFAC(N-p_a-b+2); } } } } cout << ans.v << "\n"; }
Richard's N2N2 code:
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9+7; /** * Description: Modular arithmetic. * Source: KACTL * Verification: https://open.kattis.com/problems/modulararithmetic * Usage: mi a = MOD+5; cout << (int)inv(a); // 400000003 */ struct mi { // WARNING: needs some adjustment to work with FFT int v; explicit operator int() const { return v; } mi():v(0) {} mi(ll _v):v(int(_v%MOD)) { v += (v<0)*MOD; } }; mi& operator+=(mi& a, mi b) { if ((a.v += b.v) >= MOD) a.v -= MOD; return a; } mi& operator-=(mi& a, mi b) { if ((a.v -= b.v) < 0) a.v += MOD; return a; } mi operator+(mi a, mi b) { return a += b; } mi operator-(mi a, mi b) { return a -= b; } mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); } mi& operator*=(mi& a, mi b) { return a = a*b; } mi pow(mi a, ll p) { assert(p >= 0); // won't work for negative p return p==0?1:pow(a*a,p/2)*(p&1?a:1); } mi inv(mi a) { assert(a.v != 0); return pow(a,MOD-2); } mi operator/(mi a, mi b) { return a*inv(b); } bool operator==(mi a, mi b) { return a.v == b.v; } using vmi = vector<mi>; /** * Description: pre-compute factorial mod inverses, * assumes $MOD$ is prime and $SZ < MOD$. * Time: O(SZ) * Source: KACTL * Verification: https://dmoj.ca/problem/tle17c4p5 */ vmi invs, fac, ifac; void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; for(int i = 2; i < SZ; i++) invs[i] = mi(-(ll)MOD/i*(int)invs[MOD%i]); for(int i = 1; i < SZ; i++) fac[i] = fac[i-1]*i, ifac[i] = ifac[i-1]*invs[i]; } mi comb(int a, int b) { if (a < b || b < 0) return 0; return fac[a]*ifac[b]*ifac[a-b]; } mi FAC(int a){ if(a < 0) return mi(0); return fac[a]; } mi IFAC(int a){ if(a < 0) return mi(0); return ifac[a]; } int main() { genFac(20005); int N, X; cin >> N >> X; mi ans = mi(0); for(int b = 1; b <= N; b++){ for(int p_c = 1; p_c <= X; p_c++){ ans+=(comb(N+p_c-X-1, b-2)-comb(p_c-1, b-2))*FAC(b-1)*FAC(N-b)*mi(X-p_c)*invs[2]; } } for(int b = 1; b <= N; b++){ for(int p_a = X+1; p_a <= N; p_a++){ ans+=mi(X)*FAC(N-p_a)*FAC(N-b)*IFAC(N-p_a-b+2)*mi(b-1); } } cout << ans.v << "\n"; }
We now present an alternative way to optimize counting normal HILOs from O(N3)O(N3) to O(N)O(N). ∑b,pc,pa∣1≤b≤N,pc<x+0.5<pa(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!⋅(x−pc)⋅(b−12)∑b,pc,pa∣1≤b≤N,pc<x+0.5<pa(N−pa+pc−1)!(N−b)!(N−pa+pc−b+2)!⋅(x−pc)⋅(b−12).
Let pa−pc=dac.pa−pc=dac. Then, our sum is equal to ∑dac∑Nb=1∑pc(N−d−1)!(N−b)!(N−d−b+2)!⋅(x−pc)⋅(b−12)∑dac∑b=1N∑pc(N−d−1)!(N−b)!(N−d−b+2)!⋅(x−pc)⋅(b−12), where pa,pcpa,pc must satisfy 1≤pc<x+0.5<pa≤N.1≤pc<x+0.5<pa≤N.
Consider the set of all pairs (pa,pc)(pa,pc) such that the above condition holds for some fixed value of dac=pa−pc.dac=pa−pc. Let f(dac)f(dac) be the size of the set, and let g(dac)g(dac) be the sum over all such pairs of pc.pc. Both of these values can be computed in O(1)O(1) for each value of dac.dac.
Then, the sum is equal to ∑N−1dac=1∑Nb=1(N−dac−1)!(N−b)!(N−dac−b+2)!⋅(x⋅f(dac)−g(dac))⋅(b−12)∑dac=1N−1∑b=1N(N−dac−1)!(N−b)!(N−dac−b+2)!⋅(x⋅f(dac)−g(dac))⋅(b−12) =∑N−1dac=1(x⋅f(dac)−g(dac))(N−dac−1)!∑Nb=1(N−b)!(N−dac−b+2)!⋅(b−12)=∑dac=1N−1(x⋅f(dac)−g(dac))(N−dac−1)!∑b=1N(N−b)!(N−dac−b+2)!⋅(b−12)
If we ignore the (b−12)(b−12) term in the inner loop, then the inner loop could easily be removed by an application of Hockey Stick (similar to the application mentioned earlier). To do this, note that (b−12)=C1(N−b+1)(N−b+2)+C2(N−b+1)(b−12)=C1(N−b+1)(N−b+2)+C2(N−b+1) for constants C1,C2C1,C2, so
∑Nb=1(N−b)!(N−dac−b+2)!⋅(b−12)=C1∑Nb=1(N−b+2)!(N−dac−b+2)!+C2∑Nb=1(N−b+1)!(N−dac−b+2)!∑b=1N(N−b)!(N−dac−b+2)!⋅(b−12)=C1∑b=1N(N−b+2)!(N−dac−b+2)!+C2∑b=1N(N−b+1)!(N−dac−b+2)!.
Now, similar to before, we can apply Hockey Stick to calculate both of these sums in O(1)O(1) time.
Thus, we have counted both special and normal HILOs in O(N)O(N) time, leading to a O(N)O(N) time solution.
----
As Rowechen Zhong notes here, there is actually a simple formula for the number of HILOs (assuming 0<x<N0<x<N):
where Hi≜∑ij=11jHi≜∑j=1i1j. Code:
... mi H(int x) { mi ans = 0; for (int i = 1; i <= x; ++i) ans += invs[i]; return ans; } int main() { int N,x; cin >> N >> x; if (x == 0) { cout << "0\n"; exit(0); } genFac(N+1); mi expected = (H(x)+H(N-x)-H(N)+(mi)(N-x)*invs.at(N))/2; cout << (fac.at(N)*expected).v << "\n"; }
Here is a different way to come up with the above formula for E[#(HILO)]E[#(HILO)] (as well as E[#(LOLO)],E[#(LOHI)],E[#(LOLO)],E[#(LOHI)], and E[#(HIHI)]E[#(HIHI)]):
Recall from the work above that
Note that our notation here is slightly different than above; here, a,b,ca,b,c denote values rather than positions. We can actually count the number of normal HILOs directly with a triply nested for loop:
... int main() { int N,x; cin >> N >> x; if (x == 0) { cout << "0\n"; exit(0); } genFac(N+1); mi expected = (N-x)*invs.at(N); // special HILOs for (int a = 1; a <= x; ++a) // normal HILOs for (int b = x+1; b <= N; ++b) for (int c = a+1; c <= x; ++c) { // probability that we see LO at a, then HI at b, then LO at c // only depends on relative ordering of values in a..b const int len = b-a+1; expected += invs.at(len)*invs.at(len-1)*invs.at(len-2); } cout << (expected*fac.at(N)).v << "\n"; }
Given the value of E[#(HILO)]E[#(HILO)], we can get the expected number of LOHIs:
Define a HILOLO triple (a,b,c)(a,b,c) to be values satisfying b<c≤x<ab<c≤x<a such that aa appears before bb before cc, bb and cc are two consecutive LOs, and aa is the latest HI that appears before bb We can get the expected number of LOLOs by adding the number of HILOLOs with the number of LOLOs with no HI preceding it. The code to count the expected number of HILOLOs turns out to be identical to the code used to compute the expected number of LOHILOs above, so:
The equality E[#(LO before first HI)]=∑xi=1(1N−x+i)E[#(LO before first HI)]=∑i=1x(1N−x+i) can be derived by starting with a permutation of the values x+1…Nx+1…N and then consider adding each of the values x…1x…1 to the permutation in that order. x+1−ix+1−i is a LO only if it precedes all of the values x+2−i…Nx+2−i…N, which occurs with probability 1N−x+i1N−x+i.
By symmetry,
Also,
Combining these four equations, we get the following closed forms:
[/hide]
翰林课程体验,退费流程快速投诉邮箱: yuxi@linstitute.net 沪ICP备2023009024号-1