Created
February 14, 2013 13:03
-
-
Save joriki/4952608 to your computer and use it in GitHub Desktop.
Find permutations that lead to uniform assignments upon removing cards from an arrangement; see http://math.stackexchange.com/questions/302551.
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
import java.util.List; | |
import java.util.ArrayList; | |
public class Question302551 { | |
final static boolean removeOne = false; | |
public static void main (String [] args) { | |
int n = Integer.parseInt (args [0]); | |
int k = Integer.parseInt (args [1]); | |
long multiplicity; | |
int max; | |
if (removeOne) { | |
if (n % k != 0) { | |
System.out.println ("integer multiplicity required"); | |
return; | |
} | |
multiplicity = n / k; | |
max = (int) Math.pow (n-k+1,k-1); | |
} | |
else { // take all but one | |
multiplicity = 1; | |
for (int i = n - k + 2;i <= n;i++) | |
multiplicity *= i; | |
for (int i = 2;i <= k;i++) { | |
if (multiplicity % i != 0) { | |
System.out.println ("integer multiplicity required"); | |
return; | |
} | |
multiplicity /= i; | |
} | |
max = n; | |
} | |
System.out.println ("multiplicity : " + multiplicity); | |
int count = 0; | |
outer: | |
for (int [] p : getPermutations (k)) { | |
int [] multiplicities = new int [max]; | |
for (int [] c : getCombinations (n,k)) { | |
int r = 0; | |
for (int i : c) | |
r += i; | |
r = p [r % k]; | |
int index; | |
if (removeOne) { | |
index = 0; | |
for (int j = 0;j < k;j++) | |
if (j != r) { | |
index *= n - k + 1; | |
index += c [j] - j; | |
} | |
} | |
else | |
index = c [r] - r; | |
if (++multiplicities [index] > multiplicity) | |
continue outer; | |
} | |
if (removeOne) { | |
for (int i : p) | |
System.out.print (" " + i); | |
System.out.println (); | |
} | |
count++; | |
} | |
System.out.println (count + " permutations"); | |
} | |
static int [] [] getCombinations (int n,int k) { | |
List<int []> combinations = new ArrayList<int[]> (); | |
int [] combination = new int [k]; | |
for (int i = 0;i < k;i++) | |
combination [i] = i; | |
for (;;) { | |
combinations.add (combination.clone ()); | |
int j = k; | |
int m = n; | |
do | |
if (j == 0) | |
return combinations.toArray (new int [0] []); | |
while (++combination [--j] > --m); | |
while (++j < k) | |
combination [j] = combination [j - 1] + 1; | |
} | |
} | |
static int [] [] getPermutations (int n) { | |
return getPermutations (n,n); | |
} | |
static int [] [] getPermutations (int n,int k) { | |
List<int []> permutations = new ArrayList<int[]> (); | |
makePermutations (permutations,new int [k],new boolean [n],0); | |
return permutations.toArray (new int [permutations.size ()] []); | |
} | |
static void makePermutations (List<int []> permutations,int [] permutation,boolean [] used,int j) { | |
if (j == permutation.length) | |
permutations.add (permutation.clone ()); | |
else | |
for (int i = 0;i < used.length;i++) | |
if (!used [i]) { | |
permutation [j] = i; | |
used [i] = true; | |
makePermutations (permutations,permutation,used,j + 1); | |
used [i] = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment