Problem Statement
- Place a token onto the root vertex r.
- While the token is on a red vertex, choose one of its two children at random with equal probability and move the token onto the chosen child.
- When the token is on a white vertex for the first time, color that vertex red and remove the token.
Definition
- Class:
- RndSubTree
- Method:
- count
- Parameters:
- int
- Returns:
- int
- Method signature:
- int count(int k)
- (be sure your method is public)
Constraints
- k will be between 1 and 2000, inclusive.
Examples
1
Returns: 0
With only one red vertex there are no paths, so their total length is always zero.
2
Returns: 1
The two red vertices will always be the root vertex r and one of its children. The only path with both endpoints red will always have length 1.
3
Returns: 4
Regardless of the random choices, the three red vertices will always form a path. (The root vertex r may be an endpoint of this path, or it can be in its middle.) The three random paths will have lengths 1, 1, and 2, for a total of 4.
4
Returns: 875000016
With probability 7/8 the sum of path lengths is 10 and with probability 1/8 the sum of path lengths is 9. Thus, the expected value of the sum of path lengths is (7/8)*10 + (1/8)*9 = 79/8. The multiplicative inverse of 8 modulo M = 10^9 + 7 is 125,000,001, because (125000001*8) mod M = 1. Thus, the value you should return is (79 * 125000001) mod M = 875000016.
5
Returns: 531250023
13
Returns: 550543927
6
Returns: 536132849
10
Returns: 582619904
14
Returns: 989090268
963
Returns: 683171333
465
Returns: 715789616
1706
Returns: 824216557
146
Returns: 117331447
1282
Returns: 804393528
828
Returns: 892514529
1962
Returns: 711362666
492
Returns: 750679241
996
Returns: 561537129
1943
Returns: 284057289
828
Returns: 892514529
1437
Returns: 836650432
392
Returns: 291294092
605
Returns: 641435326
1903
Returns: 760951976
154
Returns: 611633578
293
Returns: 100983582
383
Returns: 693880629
1422
Returns: 526846566
717
Returns: 933463998
2000
Returns: 201217106
1997
Returns: 566030206
1972
Returns: 66744704