2018-09-30 22:01:58 +00:00
---
id: 5900f4481000cf542c50ff5a
title: 'Problem 219: Skew-cost coding'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 301861
2021-01-13 02:31:00 +00:00
dashedName: problem-219-skew-cost-coding
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2021-07-15 07:20:31 +00:00
Let $A$ and $B$ be bit strings (sequences of 0's and 1's).
2020-11-27 18:02:05 +00:00
2021-07-15 07:20:31 +00:00
If $A$ is equal to the < u > left< / u > most length($A$) bits of $B$, then $A$ is said to be a prefix of $B$.
2020-11-27 18:02:05 +00:00
2021-07-15 07:20:31 +00:00
For example, 00110 is a prefix of < u > 00110< / u > 1001, but not of 00111 or 100110.
2018-09-30 22:01:58 +00:00
2021-07-15 07:20:31 +00:00
A prefix-free code of size $n$ is a collection of $n$ distinct bit strings such that no string is a prefix of any other. For example, this is a prefix-free code of size 6:
2018-09-30 22:01:58 +00:00
2021-07-15 07:20:31 +00:00
$$0000, 0001, 001, 01, 10, 11$$
2018-09-30 22:01:58 +00:00
2021-07-15 07:20:31 +00:00
Now suppose that it costs one penny to transmit a '0' bit, but four pence to transmit a '1'. Then the total cost of the prefix-free code shown above is 35 pence, which happens to be the cheapest possible for the skewed pricing scheme in question. In short, we write $Cost(6) = 35$.
2018-09-30 22:01:58 +00:00
2021-07-15 07:20:31 +00:00
What is $Cost(10^9)$?
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-07-15 07:20:31 +00:00
`skewCostCoding()` should return `64564225042` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-07-15 07:20:31 +00:00
assert.strictEqual(skewCostCoding(), 64564225042);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-07-15 07:20:31 +00:00
function skewCostCoding() {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-07-15 07:20:31 +00:00
skewCostCoding();
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
// solution required
```