私はビットのすべてのユニークな組み合わせを取得しようとしています(本当に興味深い..私は知っています..zzz)。基本的に、3つの一意の10ビットまたは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でも、非常に迅速に正しい結果をもたらします。ただし、数値が高くなると、処理に時間がかかるのとは対照的に、結果が得られません)