我正在尝试获得所有独特的位组合(真正有趣......我知道......)。基本上它适用于小数字,如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 ..但随着数字越来越高,它就不会给出结果而不是花时间处理)
答案 0 :(得分:4)
由于您使用的是long
,因此限制为64位。但是,200
选择30
将会根据一些粗略的背后数学信息 - 在现代PC上迭代10亿亿美元。