Here is a demonstration of the Dancing Links idea (Wikipedia definition).

The idea is this: if you remove an item from a doubly-linked list, you can put it right back in as long as you hold on to it. And, if you remove items , you can put them right back as long as you reverse the order exactly, .

This is done as follows:

```// Remove: `curr` is forgotten by neighbors
curr->prev->next = curr->next;
curr->next->prev = curr->prev;

// Reinsert: 'curr' knows where to go, and steps right back in!
curr->next->prev = curr;
curr->prev->next = curr;

// Works over and over, as long as it's done in stack order:
// remove=push, reinsert=pop.
```

This works well where you’re drilling in to search but need to backtrack too — you can do so without the need to work with multiple copies.

And finally, this is demonstrated in git and as follows:

# Rectangle Intersections on a Large Scale

I‘ve put together the following C++ code to detect rectangle intersections in time ,

Additionally, I amended to include all touching (edge-to-edge, corner-to-corner), with no change in running time.

The code is in git and below:

# Power the Power Up

This post is in reference to SPOJ 21690. Power the Power Up POWERUP.

How do you solve where is prime?

Reality check: you can’t just crunch the numbers since this might be .

First, the highest part must be chopped down. Fortunately our answer is taken modulo a prime number. So if we can solve for where , we can safely take .

E.g. if , then ; these exponents disappear.

This is Fermat’s Little Theorem (a.k.a. phi function, totient), giving us when is prime.

So, calculate using modular exponentiation. Then do it again on with . This operation is as follows:

```typedef unsigned long long uint64;
uint64 modular_pow(uint64 base, uint64 exp, uint64 mod) {
uint64 result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 == 1) {
result *= base;
result %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return result;
}
```

But wait! There’s one loose end: what if we can’t solve ? This case too must be handled but I’ll leave that to you.

# Curd Producers

This post is in reference to SPOJ Curd Producers CURDPROD.

You have several machines, each producing one item at its own speed. One might produce every 5 seconds, another every 1000.

The question: what is the minimum time required to produce T items?

The first calculation required is: how many items were produced by time X? This is a simple integer division calculation, as follows:

```uint64 production(vector<uint32>& machines, uint64 time) {
uint64 total = 0;
for (uint32 m_ix = 0; m_ix < machines.size(); m_ix++) {
total += time / machines[m_ix];
}
}
```

Also, in the problem the maximum time is limited to . So, we have a bounded search space where the result at a given time is easy to calculate. What we've got here is a binary search.

My solution is in git and below:

# Palindrome

This post is in reference to CodeForces Palindrome.

The question is: given a string, can you find the longest palindrome sub-sequence?

N.B. A sub-sequence of a string is what remains after removing any characters you’d like, keeping the order unchanged. Sub-sequences of are , , , , etc. The following are not: , , , .

The solution uses dynamic programming on a 2D vector, with input string along both the top and side. The number of cells is .

Grid[row][col] means “longest sub-sequence within input sub-string [col…row]”. The grid is calculated from top to bottom, each row from right to left.

Example input =

Explanations:

N.B. LCPal is Longest Common Sub-Sequence Palindrome

1. LCPal of “” is “
2. LCPal of “” is “
3. LCPal of “” is “” (taken from row above)
4. LCPal of “” is “” (take the match plus row/column above/beside, which is empty)
5. LCPal of “” is “” (taken from column beside)
6. LCPal of “” is “” (taken from row above)
7. LCPal of “” is “” (take the match plus row/column above/beside), and that’s the final answer

And an implementation note: because each row only requires the previous, I don’t keep all rows in memory — just two.

My solution is in git and below:

This post is in reference to CodeForces Stadium and Games.

Let’s look at a subset of inputs, and see what shakes out.

N.B. From problem description

– Phase 1: total of halves removed
– Phase 2 input: number of teams minus phase one
– Phase 2 output: phase-2-input * ( phase-2-input — 1 )/2

```x (binary)  phase-1 phase-2-input
1 (1b)      =  0       1
3 (11b)     =  0       3
5 (101b)    =  0       5
7 (111b)    =  0       7
9 (1001b)   =  0       9
11 (1111b)  =  0       11

x (binary)  phase-1 phase-2-input
2 (10b)     =  1       1
6 (110b)    =  3       3
10 (1010b)  =  5       5
14 (1110b)  =  7       7
18 (10010b) =  9       9
22 (11110b) =  11      11

...

x (binary)  phase-1 phase-2-input
8 (1000b)     =  7       1
24 (11000b)   =  21      3
40 (101000b)  =  35      5
56 (111000b)  =  49      7
72 (1001000b) =  63      9
88 (1111000b) =  77      11
```

First, notice the phase-2-input in each case: it’s strictly increasing. And phase-2-output (on this strictly increasing input) is strictly increasing. And phase 1 is strictly increasing too. Therefore, it’s safe to say that the total result on any series of inputs having the same number of ending-zeroes is strictly increasing.

This means we can perform a binary search on each group of partitioned inputs, for the given result. So, explicitly:

– Series 0: 1, 3, 5, 7… with no zeroes attached to the back
– Series 1: 1, 3, 5, 7… with 1 zero attached (yielding 2, 6, 10…)
– Series 2: 1, 3, 5, 7… with 2 zeroes attached
– etc.

Also notice that any number is part of exactly one series: if it ends in 1 zero it’s in series 1, ends in 14 zeroes it’s in series 14, etc. Therefore, all possible answers are searched.

(Also, notice the phase-1 to phase-2-input ratio in each series is the same. This is useful during implementation.)

With that, here it is in git and below:

# Eat All the Brownies

This post is in reference to SPOJ 17626. Eat all the brownies CUTCAKE.

The question is: to divide a plane into X pieces, how many lines (cuts) are needed? Magically, the answer is found on page 499 of Knuth’s Concrete Mathematics 2nd Ed. (or p485 in this PDF), and is as follows:

N.B. is the binomial coefficient function.

So this solves for regions given the number of cuts. But in this problem we’re to solve it the other way around; this is done via binary search.

My solution is in git and here:

# The Bridge to Home

This post is in reference to SPOJ 17660 The Bridge to Home WAYHOME.

You have a group of people on one side of a river, and it may only be crossed by a bridge; the bridge can take 1 or 2 at a time. All bridge-crossers must carry the lone flashlight, meaning each trip across must alternate — flashlight goes one way then back. Each person has a given “time to cross”, and two people crossing takes the longest time of the two. What is the minimum time (T) to cross?

e.g. You have times-to-cross of . So a solution is as follows:

across = 10
back = 3
across = 5

The algorithm has several cases as follows:

1. Remaining < = 2: simply cross

2. Remaining == 3: finish up, lo & mid, lo back, lo & hi

3. Remaining > 3: shuttle two highest
lo = lowest
lo2 = 2nd lowest
hi2 = 2nd highest
hi = highest

N.B. Take the best of the two below. Using below for clarity.

3a. chauffeurs across, comes back and does the same for :

3b. chauffeurs and returns, and cross and (aha!) returns:

My solution is in git and below: