我正在尝试获得所有独特的位组合(真的很有趣..我知道..zzz)。基本上它适用于小数字,如 10 位和 3 个唯一或 100 位和 4 个唯一。我正在大规模测试它,但是当我使用 200 位和 30 个项目时会发生一些奇怪的事情……我什么也没得到。我试图调试代码,但不明白为什么。
我正在使用所有长,所以我不知道为什么,位是否受到某种限制,或者这是我的数学问题?另外,我正在为非常多的位做可能还是有一些限制(即在我的程序完成之前太阳燃烧)?理想情况下,我想从 2000 个列表中获取 30 个唯一位。这种方法是唯一超过 100 个的方法,但我不知道它的限制。
这是代码:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class Combinatorics {
static final class CombinationsIterator implements Iterator<Long> {
private int n;
private int k;
private long next;
public CombinationsIterator(int n, int k) {
this.n = n;
this.k = k;
next = (1L << k) - 1;
}
@Override
public boolean hasNext() {
return (next & (1L << n)) == 0;
}
@Override
public Long next() {
long result = next;
long x = next;
long u = x & -x;
long v = u + x;
x = v + (((v ^ x) / u) >> 2);
next = x;
return result;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
}
static final class PermutationsIterator implements Iterator<List<Integer>> {
int n;
int k;
int nPk;
List<Integer> elements;
int i;
List<Integer> next;
public PermutationsIterator(int n, int k) {
this.n = n;
this.k = k;
nPk = permute(n, k);
List<Integer> elements = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
elements.add(i);
this.elements = Collections.unmodifiableList(elements);
}
@Override
public boolean hasNext() { return i < nPk; }
@Override
public List<Integer> next() {
List<Integer> next = new ArrayList<Integer>();
List<Integer> notNext = new ArrayList<Integer>(elements);
int r = i;
int np = nPk;
for (int j = 0; j < k; j++) {
np /= n - j;
next.add(notNext.remove(r / np));
r %= np;
}
i++;
return next;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
}
public static long toCombination(List<Integer> permutation) {
long combination = 0;
for (int i : permutation)
combination |= (1L << i);
return combination;
}
public static List<Integer> toPermutation(long combination) {
List<Integer> permutation = new ArrayList<Integer>();
long combinationRemaining = combination;
int i = 0;
while (combinationRemaining > 0) {
if ((combinationRemaining & 1) > 0) {
permutation.add(i);
}
combinationRemaining >>= 1;
i++;
}
return permutation;
}
public static SortedMap<Integer, Integer> multiplicitiesOf(List<Integer> multiset) {
SortedMap<Integer, Integer> multiplicities = new TreeMap<Integer, Integer>();
for (Integer k : multiset) {
Integer v = multiplicities.get(k);
v = (v == null) ? 1 : (v + 1);
multiplicities.put(k, v);
}
return multiplicities;
}
public static Iterator<Long> combinationsIterator(int n, int k) {
return new CombinationsIterator(n, k);
}
public static Iterator<List<Integer>> permutationsIterator(int n, int k) {
return new PermutationsIterator(n, k);
}
public static int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
public static int permute(int n, int k) {
int result = 1;
for (int i = n - k + 1; i <= n; i++)
result *= i;
return result;
}
public static int choose(int n, int k) {
return permute(n, k) / factorial(k);
}
public static void main(String[] args) throws IOException {
System.out.println("Starting");
for (Iterator<Long> it = combinationsIterator(200, 2); it.hasNext(); ) {
long next = it.next();
System.out.format("%d\t%10s\n", next, Long.toBinaryString(next));
}
}
}
在main
更改方法combinationsIterator(200, 2);
中会产生奇怪的行为(10,3 很快就给出了正确的结果,甚至 100,5 ..但是随着数字变高它只是没有给出结果而不是花时间处理)