Annie has a very long line of cards, numbered from 1 to 2016, such that every odd card is face-up and every even card is face-down. The only way she can modify the line is by taking any segment of consecutive cards that has an even number of face-up cards and reversing the order of the cards in this segment (note that none of the cards are flipped). She can make modifications as many times as she wants.

Annie was sad after BaSzAt used a parity argument to convince her that it was impossible to have all face-up cards be next to each other. But, as they say, one door closes, many others open. Make Annie happy again by showing her how many different arrangements she can make with the line of cards.

Note: Two arrangements are considered distinct if a position contains a face-up card in one and a face-down card in another. That is to say, all face-up cards and face-down cards are indistinguishable.

$\begingroup$- by the way that would be for exactly one modification, no less no more.$\endgroup$
– Jonathan AllanApr 13 '16 at 15:14

$\begingroup$@Jonathan Allan, I might be wrong, but I think you missed the fact, that the segment to be reserved does not have to start or finish with a face-up card, which introduces a multiplicative factor. I don't think your expression is correct for n=4, where we are starting from an UDUD-sequence, for which one possible reverse by the endpoints 1 and 3 ends in UDUD, while another possibility is with the endpoints 1 and 4, resulting in DUDU. These are considered to be two different arrangements, while your expression suggests 2 choose 2 = 1. Am I missing something?$\endgroup$
– eliasApr 13 '16 at 15:32

$\begingroup$@elias That is counting arrangements distinct from each other and the original, to include such possibilities as your reversal of 1-3 just add one (the original sequence).$\endgroup$
– Jonathan AllanApr 13 '16 at 15:37

2 Answers
2

Based on both hints, and using the results of @Jonathan Allan's answer, I think I finally got an answer in the general form of

$\binom{\frac{n}2-1}{\frac{n}4}\cdot\binom{\frac{n}2}{\frac{n}4}$, where $n$ is the number of cards, divisible by $4$.
For $n=2016$ this gives $\binom{1007}{504}\cdot\binom{1008}{504}$, which equals the huge number suggested by @Jonathan Allan.
I haven't tried to generalize the formula for $n$s which don't have a factor of $4$.
My reasoning below uses some ideas which need that the number of face-up cards, which is $\lceil n/2 \rceil$, to be even.
Fortunately - and probably not at all accidentally - the actual case of $n=2016$ satisfies this comfortable condition. Even better, in this case $n/2$ is integer itself, no need for the ceiling.

Short explanation:

Divide the face-down cards in two groups according to if they have a total number of even or odd face-up cards in the line to the left of them.
With Annies moves, the cardinality of these groups does not change, and if two card-permutation have the same cardinalities, they can be reached with an iteration of valid reversals.
Thus the answer is how many permutations of $n/2=1008$ identical face-up and $n/2=1008$ identical face-down cards are there where $n/4=504$ face-down cards have an even number of face-up cards to the left of them, and $n/4=504$ have an odd number of these, just as in the initial setup of the alternating sequence of face-up, face-down cards.

Detailed explanation:
The second hint almost gives away

where to look for the solution:
it was a natural idea to divide the face-up cards in two groups, by turns based on the natural order they appear, so every oddth face-up card is in one group, and the eventh face-up cards are in the other.
If there are $n$ cards in total, we have $n/2$ face-up cards, now divided in two groups of $n/4$.
For a better understanding, let me mark face-down cards with '-', an oddth face-up card with 'O', and an eventh face-up card with 'E'.
With this notation the original setup of the $2016$ cards looks like
O-E-O-E-O-E-...O-E-
Furthermore, generally any setup of the $n$ cards - even the ones which cannot be reached from the original setup - consists of $n/2$ '-'s, and $n/4$ 'O's and $n/4$ 'E's. In valid setups 'O's and 'E's follow each other by turns, starting with an 'O'.
For example the following is a valid setup for $n=12$:
--O-E--OEO-E
(Please note, that after a reversal, the 'O's and 'E's need to be fixed, that is, after reversing the sequence from the second to the ninth card in the above setup does not lead to
-EO--E-O-O-E
, but to
-OE--O-E-O-E
.)

So what happens during a reversal?
At this point, it is important to notice, that

the original condition of having an even number of face-up cards in the sequence to be reversed means, that if and only if the beginning of the sequence to be reserved is after an oddth face-up card, but before its consecutive eventh face-up card, then the end of the sequence will fall also after an oddth and before the next eventh one.
That is, if I mark the start and end of the sequence to be reversed with '|', either both '|'s are between an 'O' and its following 'E' (and maybe some '-'s which we can ignore now), or both are between an 'E' and its following 'O' (minor remark: or it can be the special case of being before the first 'O' or after the last 'E', but these cases behave totally regularly as well, no trick needed).
So it either looks like something like this:
-OE--O|-EO-E-O--|E
or this:
-|OE--O-EO-E-|O--E
We will come back to this soon, but have to do a short intermission.

The first hint strengthened my impression, that

this is a typical problem which involves finding conservative quantities.
Let's focus on the number of '-'s, and let's divide those in two groups too.
Some of those are in between an 'O' and the next 'E', while others fall after an 'E' but before its consecutive 'O' (or again, are at the very beginning/end of the line).
Let these be our two groups, which I will refer to as "being in an 'OE'-sequence" and "being in an 'EO'-sequence", respectively.
How do the cardinalities of these groups change during a reversal?
They don't! (Hooray!)
That's easier to understand if you now look back at the two chosen sequences in the previous paragraph, and realize, that '-'s which were in an 'OE'-sequence, will be in an 'OE' sequence after the reversal too, as the parity of the last face-up card in the sequence to be reversed is the same as the parity of the last face-up card before the sequence, and so on.

We also have to show, that

two setups which have the same number of face-down cards being in an 'OE'-sequence (and the same number of face-down cards being in an 'EO'-sequence, and the same number of face-up cards) can be actually reached from each other by legal moves of Annie.
I need to add here, that because of the invertibility of the reversing it does not really matter which setup is the one we are trying to reach from the other one. If there is a setup $C$ which we can reach from both $A$ and $B$, then $B$ can be reached from $A$ as well, with the sequence of reversals leading from $A$ to $C$, and the reversed sequence of reversals leading from $B$ to $C$.
Let's consider two setups, which are both valid, having the same number of these 'OE'-sequence face-down cards, but are not identical.
This latter means that there is at least one position, in which one of them has a face-up card, and the other has a face-down card. Let's look at the leftmost of these positions.
The setup with the face-down card in this position has to have at least two face-up cards to the right from this position, otherwise the two setups could not have the same amount of face-down cards being in an 'OE'-sequence.
So we can reverse this setup at the sequence starting from the face-down card being at the leftmost difference's position, and ending at the second face-up card to the right from there.
After the reversal we get a setup which will also have a face-up card in the position where there was a difference before (because of the face-down card being there), so we managed to resolve the leftmost difference without changing anything to the left of it. Thus if there is still a difference between the newly-got and the other setup, it has to be in a position which is to the right from this previous difference. As the length of the setups is finite, repeating this iteratively, we will actually end up having the same setups using valid moves.

So there is only the enumeration left.

In the setup which Annie starts from (O-E-O-E-...O-E-), there are just as many '-'s in 'OE'-sequences as in 'EO'-sequences, both $n/4=504$.
There are also $n/4=504$ 'O's followed by an 'E', that is, $n/4=504$ places to put '-'s into an 'OE' sequence.
The number of 'E's followed by an 'O' is $n/4-1=503$, but a '-' is in an 'EO'-sequence also at the very beginning or at the very end of the line, so that means $n/4-1+2=n/4+1=505$ possible places.
Because of these placements can be made independently of each other, the number of all the matching configuration can be calculated by multiplicating the two combinations with repetitions: $\binom{\frac{n}{4}+\frac{n}{4}-1}{\frac{n}{4}}\cdot\binom{(\frac{n}{4}+1)+\frac{n}{4}-1}{\frac{n}{4}}=\binom{1007}{504}\cdot\binom{1008}{504}$

EDIT: Solution for every $n$

How does the number of face-up and face-down cards, the parity of the former, the cardinality of the groups of the latter, and thus the expression change when $n$ is not divisible by $4$?
I think all the reasoning still holds, the only difference is, that $n/4$ is not an integer.
The number of face-up cards is $\lceil \frac{n}{2} \rceil$;
they always start with the first (an oddth one), so the number of oddth face-up cards is $\lceil \frac{n}{4} \rceil$,
while the number of eventh face-up cards is $\lceil \frac{n-2}{4} \rceil$.
The number of face-down cards in the default setup is $\lceil \frac{n-1}{2} \rceil$;
$\lceil \frac{n-1}{4} \rceil$ of them being in an 'OE'-sequence,
and the rest $\lceil \frac{n-3}{4} \rceil$ being in an 'EO'-sequence.
The former can be rearranged after any of the 'O's, that's $\lceil \frac{n}{4} \rceil$ possible positions,
while the others can be put after every 'E', or to the very beginning, giving $\lceil \frac{n-2}{4}+1 \rceil$ possibilities.
The final formula changes to $\binom{\lceil \frac{n}{4} \rceil + \lceil \frac{n-1}{4} \rceil - 1}{\lceil \frac{n-1}{4} \rceil}\cdot\binom{\lceil \frac{n-2}{4}+1 \rceil + \lceil \frac{n-3}{4} \rceil - 1}{\lceil \frac{n-3}{4} \rceil}$.

Notice that the generic formula

matches @Jonathan Allan's brute force calculations for even $n$s, and also supports his idea for $n$s which have a remainder of $3$ divided by $4$: for those $f(n)=f(n+1)/2$.
However, for $n$s which are $1$ modulo $4$: $f(n)=f(n-1)\cdot2$ seems to hold.
A counterexample for his calculation is $n=5$, where $f(n)=4$ (instead of $3$), and the reachable setups are UDUDU, DUDUU, UUDUD, and DUUUD

$\begingroup$In the "We also have to show, that" section (the bit I was struggling with for many different properties including this one), we need to do some more work. Consider these two (valid and reachable) arrangements when n=12: UDDUDUUUDUDD and UDDUUDUDUUDD. The longest differing sub-sequences are DUUUD vs UDUDU (not meeting either the "one must start U and end D", or "the number of Us are even" criteria); I chose a case where it is fairly obvious we can reach one from the other (the sub-sequences are two valid sequences at n=5, where the issue first occurs - but here n is also divisible by 4).$\endgroup$
– Jonathan AllanApr 16 '16 at 3:10

$\begingroup$My proof for that part is indeed a bit bigger (if it helps, it is a constructive proof, in that I show a procedure that turns any sequence with the property into some other sequence with the property). If no one gets it by the time the bounty expires, I'll self-answer :)$\endgroup$
– ffaoApr 16 '16 at 3:47

$\begingroup$how about this? both of the two differing longest subsequences contain at least 2 Us (otherwise the cardinality of 'OE'-Ds do not match in them), one of them has to start with D (as they are different in that position). reverse that from that D till the second U to the right from it (this has to be inside the differing subsequence). that way we've made the differing subsequence at least one shorter, as now both of those start with U.$\endgroup$
– eliasApr 16 '16 at 5:50

$\begingroup$The otherwise part needs more justification, and sounds false to me. Also, if the target sequence has a D in that position, you can't flip the target, so that needs some explaining too. This looks really close to my solution, but you're thinking too much about "longest differing subsequence", which is hard to reason about. How about "first mismatched position"?$\endgroup$
– ffaoApr 16 '16 at 15:07

Given some even number of cards, $n=p\times 2$, the number of possible arrangements Annie may produce seems to fit with $\binom{p-1}{\lfloor\frac {p-1}2 \rfloor} \binom{p}{\lfloor\frac p2 \rfloor}$.

...

For odd $n$ the number of arrangements possible is:double the number possible with $n-1$ if $n\mod4 = 1$; orhalf of the number possible with $n+1$ if $n\mod4 = 3$(Matching elias's final result for $n<=24$).

Running some bit manipulation code the results I get for even numbers of cards are as follows(counts of new arrangements found at each depth of Annie's choices as laid out in the question):

$\begingroup$@Jonathan Allan, I'm afraid your formula is not correct for odd $n$s. see the final remark in my answer which addresses this in details$\endgroup$
– eliasApr 15 '16 at 14:12

1

$\begingroup$@elias - yes there was a bug in how I initialised my words when n was odd (making a face down cards come first in those cases!). Matches your results now.$\endgroup$
– Jonathan AllanApr 16 '16 at 1:43