# How many unique rows of pebbles are there?

A valid row of pebbles has $n$ white, and $n$ black pebbles. Thus, its length is $2n$.

Two rows of pebbles are considered equivalent if one can be produced from another by any combination of inversions (replacing all whites with blacks and vice versa) and flips (reverse the order of pebbles).

How many unique rows of pebbles are there for some $n$?

If we want our rows of pebbles to look mixed, we can say that a mixed row of pebbles can't have more than $2$ consecutive pebbles of the same color.

(mixed case $m=1$ is trivial with only one solution per $n$)

Then, how many rows of unique mixed pebbles are there for some $n$? (mixed case $m=2$)

We can represent pebbles with $0$'s and $1$'s.
I've tried generating examples for some $n$'s by iterating binary numbers;

We have solutions for first few $n$'s, for unique rows:

$$1, 3, 7, 23, 71, 252, 890, 3299, 12283, 46508, \dots$$

Edit: Corrected thanks to Jeremy Dover from comments: Now, this sequence is in OEIS.

If we eliminate ones with $3$ or more same consecutive pebbles from previous examples;

Then we have the number of unique mixed ones for $n=1,\dots,10,\dots$ below ($m=2$):

$$1 ,3 ,5 ,12 ,25 ,61 ,140 ,347 ,841 ,2108 , \dots$$

Edit: Also corrected. This sequence still does not appear in OEIS.

Q: How can we find these values for unique mixed examples algebraically?

P.S. Is there any significantly faster way to generate all solutions for some $n$? The outputs with all examples for the second sequence so far: pastebin.com ; Where second column is the decimal value and third is the difference between consecutive examples.

Also, would it be interesting to generalize this and find solutions for rows of size $k\times n$ where $k$ is the number of distinct colors of pebbles, and also consider $m$ consecutive pebbles allowed?
How does one calculate/compute a solution for some $(k,n,m)$?

A follow up; If we were to have $2n$ rows of pebbles together to form a "pebble garden", how many unique mixed pebble gardens are there?

That is, now we also need to check the columns, as well as rows, for the same number of pebbles and for no three consecutive pebbles of same color. We also need to consider both horizontal and vertical flips and the 90° degree rotations for uniqueness, then this follow up is equivalent to the following question:

How many distinct Unruly boards are there?

P.S. We could also stack the gardens into "pebble cubes", and continue to "pebble tesseracts", and continue to... $N$ dimensional unique pebble structures? But given the follow up has zero progress posed there as of this moment, this is perhaps still an overreach.

• For the first problem, your counts are incorrect. Looking at your pastebin for n=3, you have 100011 and 110001 counted as distinct, but they are isomorphic with respect to a flip. For n>=3, your counts seem to be off by exactly one each time. Using the corrected numbers, you can find your first sequence in the OEIS at oeis.org/A045723. Nov 21, 2017 at 15:53
• @JeremyDover That was a terribly missed error in my quick code. Thanks for noticing. I've corrected both sequences now. Nov 21, 2017 at 16:40

For starters we verify the formula that is given at OEIS A045723 which treats the problem without adjacency constraints. We solve the case of $n$ instances of $k$ colors for both $n\times k$ even and odd. What we have here is a case of Power Group Enumeration with the slot permutations consisting of the identity and the reflection. We represent the color scheme by by a sequence of $k$ blocks of $n$ identical colors, $n\times k$ in total. The object permutation group acting on these is obtained by the simultaneous action of the symmetric group $S_k$ on the $k$ blocks, which encodes the fact that the colors are swappable, and a vector of $k$ permutations $\gamma_1,\ldots, \gamma_k$ from $S_n$ acting on the constituents of the blocks, which encode the fact that the $n$ instances of the $k$ colors are the same. Denote this by $Z(F_{n,k}).$

For Power Group Enumeration we require the cycle index of the slot permutation group which is

$$Z(Q_{n,k}) = \frac{1}{2} (a_1^{nk} + a_2^{nk/2})$$

when $n\times k$ is even and

$$Z(Q_{n,k}) = \frac{1}{2} (a_1^{nk} + a_1 a_2^{(nk-1)/2})$$

when $n\times k$ is odd. We must cover these two with cycles from permutations from $Z(F_{n,k})$ where we are using a set cover, i.e. all colors must be present. Now for the first one there clearly is only one possibility, coverings using fixed points from the identity permutation. Since there are $k!\times n!^k$ permutations in $Z(F_{n,k})$ we get a contribution of

$$\frac{(nk)!}{k! \times n!^k}.$$

The power of $a_2$ requires more work and must be simplified to make for a feasible computation. Note once more that in a set cover we must use all cycles from the permutation being sourced for the cover. This clearly requires for the two permutations to have exactly the same cycle structure. Therefore we are extracting $[a_2^{nk/2}] Z(F_{n,k})$ and $[a_1 a_2^{(nk-1)/2}] Z(F_{n,k}).$ Supposing that $\beta$ is the permutation from $S_k$ we see that it must be an involution, consisting of fixed points and two-cycles only, since this is the structure of the coefficient being extracted and we cannot return to the starting point of a cycle without having made at least one turn. Now if $\beta$ fixes a certain block then the contribution from that block (permutation $\gamma$) to the cycle index enters identically and hence it must be an involution as well. One the other hand if two blocks are on a two-cycle the cycles where their elements reside must have even length wich implies they are two-cycles. This means that the corresponding permutations $\gamma_a$ and $\gamma_b$ are inverses of each other and the joint action with $\beta$ splits everything into two-cycles. Using the exponential formula we introduce

$$Z(S_{q,\mid m}) = [w^q] \exp\left(\sum_{d|m} a_d \frac{w^d}{d}\right)$$

we thus obtain

$$[a_2^{nk/2}] Z(F_{n,k}) = [a_2^{nk/2}] Z(S_{k,\mid 2}) \left(Z(S_{n,\mid 2}), \frac{1}{n!} a_2^n\right)$$

and similar for $n\times k$ odd. Here a cycle index evaluated with arguments signifies substitution into $a_1, a_2$ etc. It remains to observe that we have two choices for each cycle being covered when we cover the two-cycles and any permutation of the source cycles from $Z(F_{n,k})$ is valid. We finally get for the desired closed form when $n\times k$ is even

$$\bbox[5px,border:2px solid #00A000]{ \frac{1}{2} \left(\frac{(nk)!}{k! \times n!^k} + (nk/2)! \times 2^{nk/2} \times [a_2^{nk/2}] Z(S_{k,\mid 2}) \left(Z(S_{n,\mid 2}), \frac{a_2^n}{n!}\right) \right)}$$

and

$$\bbox[5px,border:2px solid #00A000]{ \frac{1}{2} \left(\frac{(nk)!}{k! \times n!^k} + ((nk-1)/2)! \times 2^{(nk-1)/2} \times [a_1 a_2^{(nk-1)/2}] Z(S_{k,\mid 2}) \left(Z(S_{n,\mid 2}), \frac{a_2^n}{n!}\right) \right)}$$

otherwise. We can compute these values with the following Maple code.

with(numtheory);
with(combinat);

pet_cycleind_symm :=
proc(n)
option remember;

if n=0 then return 1; fi;

end;

pet_cycleind_symm_nk_div2 :=
proc(n, k)
option remember;
local idx_col, idx_slot, res;

if n=1 and k=1 then return a[1] fi;

if n=1 or k=1 then
return pet_cycleind_symm(n*k);
fi;

idx_col := coeftayl(exp(a[1]*z+a[2]/2*z^2), z=0, k);
idx_slot := coeftayl(exp(a[1]*z+a[2]/2*z^2), z=0, n);

res :=
subs({a[1]=idx_slot, a[2]=a[2]^n/n!},
idx_col);

expand(res);
end;

EXA :=
proc(n, k)
option remember;
local idx;

if n=1 or k=1 then return 1 fi;

idx := pet_cycleind_symm_nk_div2(n, k);

if type(n*k, even) then
1/2*((n*k)!*coeff(idx, a[1], n*k) +
((n*k)/2)!*2^(n*k/2)*
coeff(idx, a[2], n*k/2));
else
1/2*((n*k)!*coeff(idx, a[1], n*k) +
((n*k-1)/2)!*2^((n*k-1)/2)*
coeff(coeff(idx, a[2], (n*k-1)/2), a[1], 1));
fi;
end;

EXB :=
proc(n, k)
option remember;
local idx;

if n=1 or k=1 then return 1 fi;

idx := pet_cycleind_symm_nk_div2(n, k);

if type(n*k, even) then
1/2*((n*k)!/k!/n!^k +
((n*k)/2)!*2^(n*k/2)*
coeff(idx, a[2], n*k/2));
else
1/2*((n*k)!/k!/n!^k +
((n*k-1)/2)!*2^((n*k-1)/2)*
coeff(coeff(idx, a[2], (n*k-1)/2), a[1], 1));
fi;
end;



We get for two swappable colors the sequence

$$1, 3, 7, 23, 71, 252, 890, 3299, 12283, 46508, \\ 176870, 677294, 2602198, \ldots$$

which matches the data from the OP. With three swappable colors we find

$$1, 11, 148, 2955, 63231, 1430912, 33259920, \\ 788827215, 18989544145, 462583897776, \ldots$$

and with four the data are

$$1, 65, 7780, 1315825, 244448316, 48099214856, \\ 9844135755168, 2074189508907945, \ldots$$

The initial segments of these may be verified with the following enumeration routine.

#! /usr/bin/perl -w
#

sub recurse {
my ($n,$k, $kf,$rest, $sofar,$orbits) = @_;

if(scalar(@$sofar) ==$n*$k){ my$sofar_rev = [ reverse(@$sofar) ]; my @orbit; for(my$idx = 0; $idx <$kf; $idx++){ my @permcols = ( 0 .. ($k-1) );

for(my ($d,$ind) = ($k,$idx);
$d > 1;$d--){
my $pos =$ind % $d; if($pos != $d-1){ my$tmp = $permcols[$pos];
$permcols[$pos] = $permcols[$d-1];
$permcols[$d-1] = $tmp; }$ind = ($ind -$pos) / $d; } my$conf = join '',  map {
chr(ord('A') + $permcols[$_])
} @$sofar; push @orbit,$conf;

$conf = join '', map { chr(ord('A') +$permcols[$_]) } @$sofar_rev;

push @orbit, $conf; } my @sorted = sort(@orbit);$orbits->{$sorted[0]} = 1; return; } for(my$col = 0; $col <$k; $col++){ if($rest->[$col] > 0){$rest->[$col]--; push @$sofar, $col; recurse($n, $k,$kf, $rest,$sofar, $orbits); pop @$sofar;
$rest->[$col]++;
}
}
}

MAIN : {
my $mx = shift || 5; my$k = shift || 2;

my $kf = 1; for(my$f=2; $f <=$k; $f++){$kf *= $f; }$| = 1;

for(my $n=1;$n <= $mx;$n++){
my $orbits = {}; recurse($n, $k,$kf, [($n) x$k],
[], $orbits); print " " if$n > 1;
print scalar(keys(%\$orbits));
}

print "\n";
}


There is another example of PGE at the following MSE link.