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

CKISSHUG - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

DIFFICULTY

SIMPLE

PREREQUISITES

Simple Math, Repeated Squaring

PROBLEM

How many sequences of 0s and 1s (0 meaning a hug, 1 meaning a kiss) of length N are possible, such that after the first 1 appears at index i, every alternate index after i - formally, i+2, i+4, i+6 and so on - is also 1. Note that this constraint is forced only by the first 1 and no subsequent 1s.

QUICK EXPLANATION

Suppose the sequence was to start with a 0, then all possible sequences of length N-1 are valid.

Now, if the sequence were to start with a 1, then every odd index in the sequence (assuming the sequence is 1-based) will be 1; the rest of the floor(N/2) indices can be either 0 or 1, mutually exclusively.

Thus we get the formula

f(N) = f(N-1) + 2floor(N/2)

Since N can be as much as 109 we cannot use this recursive formulation as it is.
The Explanation section below is about finding a closed form from the above recursive formula

EXPLANATION

Seeing the floor function and division by 2 immediately gives us the insight that we should find the formula for odd and even N.

Let fo(N) and fe(N) be the functions that are defined for the odd and even values of N respectively. Thus,
f(N) = if N is odd then fo(N) else fe(N)

(1) ... fo(N) = fe(N-1) + 2(N-1)/2
(2) ... fe(N) = fo(N-1) + 2N/2

From (1) and (2) we can derive

(3) ... fo(N) = fo(N-2) + 2(N-1)/2+ 2(N-1)/2
(4) ... fe(N) = fe(N-2) + 2N/2+ 2(N-2)/2

Where,

fo(1) = 2
fe(0) = 1

These formulas are already very usable. We can use matrix exponentiation on fo(N) or fe(N) based on whether N is even or odd. But we can do a little better and find closed form formulas of fo and fe.

Let,

to(N) = fo(N) - fo(N-2) = 2(N-1)/2+ 2(N-1)/2 = 2(N+1)/2

to(3) = 22
to(5) = 23
to(7) = 24
...
to(N) = 2(N+1)/2

Adding all of the above gives us,

fo(N) - fo(1) =[n = 0 to (N+1)/2]2n- 21- 20
fo(N) - 2 = 2(N+1)/2 + 1- 1 - 2 - 1 (summation of Geometric Progression)
fo(N) = 2(N+1)/2 + 1- 2

Similarly, we want to find the closed form for fe.

Let,

te(N) = fe(N) - fe(N-2) = 2N/2+ 2N/2 - 1

te(2) = 21+ 20
te(4) = 22+ 21
te(6) = 23+ 22
...
te(N) = 2N/2+ 2N/2 - 1

Adding all of the above gives us,

fe(N) - fe(0) =[n = 0 to N/2]2n- 20 + ∑[n = 0 to (N/2 - 1)]2n
fe(N) - 1 = 2N/2 + 1- 1 - 1 + 2N/2- 1 (summation of Geometric Progression)
fe(N) = 2N/2 + 1+ 2N/2- 2

Thus we can find the value of f(N) from the following two expressions
f(N) = 2(N+1)/2 + 1- 2, for odd N
f(N) = 2N/2 + 1+ 2N/2- 2, for even N

To further simplify the formula, you can rewrite the expressions for odd and even N as follows,

f(N) = 2(N+1)/2+ 2(N+1)/2- 2, for odd N
f(N) = 2N/2 + 1+ 2N/2- 2, for even N

Or,

f(N) = 2ceil((N+1)/2)+ 2floor((N+1)/2)- 2, for any N

The value of 2n can be found by repeated squaring.

SETTERS SOLUTION

Can be found here

TESTERS SOLUTION

Can be found here


Viewing all articles
Browse latest Browse all 39796

Trending Articles



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