Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

TAAPLUSB - Editorial

$
0
0

Problem Link:

Practice
Contest

Difficulty:

Easy

Pre-requisites:

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 ith place results in carry.
Then   E[1] = 45/100
and     E[i] = 45/100 + 10/100 * E[i-1]      for i > 1
(Justification in next section)

By linearity of expectation,

      Answer[N] = E[1] + E[2] ... E[N]

Therefore, one could first pre-compute 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[i-1])
Therefore, (0.5 - E[i]) = (0.5 - E[1]) / 10i-1 = 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 ith place results in a carry if

  1. 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.

  2. The sum of those two digits is exactly 9, and there was a carry from i-1th 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[i-1].

Hence E[i] = 45/100 + 9 * E[i-1] / 100

Setter's Solution:

Can be found here

Tester's Solution:

Can be found here


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>