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;
expand(1/n*add(a[l]*pet_cycleind_symm(n-l), l=1..n));
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.