Created
April 9, 2020 12:10
-
-
Save joriki/5b932d811c58f8f6a1d2a12bc3efcfd2 to your computer and use it in GitHub Desktop.
Find poetic derangements; see https://math.stackexchange.com/questions/3617128.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Question3617128 { | |
static long count; | |
public static void main(String [] args) { | |
for (int i = 1,n = 1;;i++,n += i) { | |
count = 0; | |
recurse (new boolean [n],new int [n],new int [i + 1],0); | |
System.out.println(n + " : " + count); | |
} | |
} | |
static void recurse (boolean [] used,int [] p,int [] counts,int i) { | |
if (i == used.length) { | |
count++; | |
for (int x : p) | |
System.out.print((char) (x + 'A')); | |
System.out.println(); | |
} | |
else | |
for (int d = 1;d < counts.length;d++) | |
if (counts [d] < d) | |
for (int sign = -1;sign <= 1;sign += 2) { | |
int j = i + d * sign; | |
if (0 <= j && j < used.length && !used [j]) { | |
used [j] = true; | |
counts [d]++; | |
p [i] = j; | |
recurse (used,p,counts,i + 1); | |
counts [d]--; | |
used [j] = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment