Ninja is playing a game, he is standing on index 1 in a linear array of size ‘N’, then he rolls a dice and moves forward according to the number on the top of the dice. He repeats this operation until he reached index ‘N’. He asked you to find the expected number of moves required to reach position ‘N’ starting from ‘1’.
Note:
1. The expected number of moves can be fractional.
2. Ninja cannot go outside the array i.e if he is at (n - 1)-th position he can only move if he gets 1 as an outcome in dice.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains one integer, ‘N’, denoting the size of the board.
Output Format:
For each test case, return a double value. If the returned value is correct with an error margin of 0.001 to -0.001. then the output will be 1 else 0.
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^9
Time Limit: 1 sec.
2
2
8
1
1
Test case 1:
Here the answer is equal to the expected number of dice rolls to get the first 1 as an outcome because we cannot go outside the array. And the expected number of dice rolls to get the first '1' is 6. i.e. On average, we need to roll a dice 6 times to get at position 2.
Test case 2.
If we are 7 steps away, then we can get from 1 by getting '6' with probability (1 / 6) and expected dice roll equal to 6, similarly, from 2 we can get 5, from 3 by 4, and so on. with all same probability i.e. 1 / 6 and expected no. of dice roll 6. Now the average value will be 1 + ( (6 * 6) / 6 = 7.
Hence, for 7 steps away, our answer is 7.
1
3
1
Test case 1:
As the Input is less than 7, the expected number of operations will be 6.
Use Matrix Exponentiaiton.
Here, as we know that dice can give us 6 moves, so we can reach a particular step from its previous 6 steps. The expected number of dice rolls required to get '1' as an outcome is 6. similarly for getting ‘2’, ‘3’, '4', '5', ‘6’ is also 6. We can reach at position ‘n’ from ‘n - 1’ by getting ‘1’, from ‘n - 2’ by getting 2, and so on. Thus the expected dice rolls required to get at position ‘n' is 1 more than the average dice roll required to get at the previous 6 positions.
The Recurrence relation can be described as:
A[n] = 1 + ( A[n - 1] + A[n - 2] + A[n - 3] + A[n - 4] + A[n - 5] + A[n - 6] ) / 6.
This can be observed as extended Fibonacci series, we can apply Matrix Exponential technique to the above relation to find the answer.
A[n - 1] = 1+ (A[n - 2] + A[n - 3] + A[n - 4] + A[n - 5] + A[n - 6] + A[n - 7]) / 6
Substracing A[n - 1] from A[n], we get.
A[n] = 7 * (A[n - 1]) / 6 - (A[n - 7]) / 6
The matrix multiplier is as follows:
[[7 / 6, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0 ],
[ 0, 0, 0, 0, 0, 1, 0 ],
[ 0, 0, 0, 0, 0, 0, 1 ],
[- 1 / 6, 0, 0, 0, 0, 0, 0 ]]
Returns the product of matrix ‘a’ and matrix ‘b’.
O( log(N)) where N is the given input.
For matrix multiplication, we need 7 ^ 3 iterations i.e. 343 iterations. For matrix exponentiation, we need (log N) interactions. Hence overall time complexity is O(343 * log(N)) which can be generalized down to O(logN).
O(1).
Here, we use constant space variables. So, our space complexity is O(1).