Problem Link:
Difficulty:
Easy
Prerequisites:
Linearity of Expectation, basic probability
Problem:
Find the expected number of carries when adding two numbers, each at most N digits long.
Explanation:
let E[i] = probability that addition at i^{th} place results in carry.
Then E[1] = 45/100
and E[i] = 45/100 + 10/100 * E[i1] for i > 1
(Justification in next section)
By linearity of expectation,
Answer[N] = E[1] + E[2] … E[N]
Therefore, one could first precompute all E[i]'s and then Answer[i]'s in O(N) time overall.
This gives complexity of O(N+T).
However, it is also possible to have O(1) time per testcase.
This can be done by rewriting the above equation as:
(0.5  E[i]) = 10/100 * (0.5  E[i1])
Therefore, (0.5  E[i]) = (0.5  E[1]) / 10^{i1} = 0.00…(i times 0)…05
So, Answer[N] = E[1] + E[2] … E[N] = 0.5 * n  0.0555…(n times 5)…5
Justifications:
Adding the digits at i^{th} place results in a carry if

The sum of those two digits is more than 9. All such pairs of digits are (1, 9), (2, 9), (3, 9) …, (2, 8), (3, 8), …, (3, 7), (4, 7), …, (9, 1). There are 45 such pairs, therefore probability of this happening is 45/100.

The sum of those two digits is exactly 9, and there was a carry from i1^{th} position. The sum of digits is 9 for the pairs (0, 9), (1, 8), (2, 7), … (9, 0). The probability of this happening is 10 / 100 * E[i1].
Hence E[i] = 45/100 + 10 * E[i1] / 100
Setter’s Solution:
Can be found here
Tester’s Solution:
Can be found here