import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; abstract class ImmutableLongList implements Iterable<Long> { public abstract boolean isEmpty(); public abstract ImmutableLongList getTail(); public abstract long getHead(); private static final ImmutableLongList empty = new ImmutableLongList() { @Override public boolean isEmpty() { return true; } @Override public ImmutableLongList getTail() { } @Override public long getHead() { } }; private class ImmutableLongListIterator implements Iterator<Long> { private ImmutableLongList currentList; public ImmutableLongListIterator(ImmutableLongList currentList) { this.currentList = currentList; } @Override public boolean hasNext() { return !currentList.isEmpty(); } @Override currentList = currentList.getTail(); return result; } @Override public void remove() { } } @Override public Iterator<Long> iterator() { return new ImmutableLongListIterator(this); } public static ImmutableLongList getEmpty() { return empty; } private static final class NonEmptyList extends ImmutableLongList { private final long head; private final ImmutableLongList tail; private NonEmptyList(final long head, final ImmutableLongList tail) { this.head = head; this.tail = tail; } @Override public boolean isEmpty() { return false; } @Override public long getHead() { return head; } @Override public ImmutableLongList getTail() { return tail; } } public static ImmutableLongList makeList(final int head, final ImmutableLongList tail) { return new NonEmptyList(head, tail); } } /** * @author User */ public class Main { final int kwota = 29; final int[] nominały = new int[]{12, 5, 7, 4, 1}; final ImmutableLongList[] L = new ImmutableLongList[kwota + 1]; final long[] W = new long[kwota + 1]; W[0] = 0; for (final int nominał : nominały) { for (int i = nominał; i < W.length; i++) { if (W[i - nominał] != pseudoInfinity) { if (W[i - nominał] + 1 < W[i]) { W[i] = W[i - nominał] + 1; L[i] = ImmutableLongList.makeList(nominał, L[i - nominał]); } } } for (int i = 0; i < W.length; i++) { final long v = W[i]; if (v == pseudoInfinity) { } else { } } } for (final long nominał : L[kwota]) { } } long objectsCount(final ImmutableLongList[] lists) { final Set<ImmutableLongList> objects = new HashSet<ImmutableLongList>(); long wynik = lists.length == 0 ? 0 : 1; // empty lists by default for (final ImmutableLongList list : lists) { ImmutableLongList currentHead = list; while (currentHead != ImmutableLongList.getEmpty()) { wynik += objects.add(currentHead) ? 1 : 0; currentHead = currentHead.getTail(); } } return wynik; } new Main().run(); } }
Standard input is empty
0, --, --, --, --, --, --, --, --, --, --, --, 1, --, --, --, --, --, --, --, --, --, --, --, 2, --, --, --, --, --, objects count: 3 0, --, --, --, --, 1, --, --, --, --, 2, --, 1, --, --, 3, --, 2, --, --, 4, --, 3, --, 2, 5, --, 4, --, 3, objects count: 12 0, --, --, --, --, 1, --, 1, --, --, 2, --, 1, --, 2, 3, --, 2, --, 2, 4, 3, 3, --, 2, 5, 3, 4, 4, 3, objects count: 18 0, --, --, --, 1, 1, --, 1, 2, 2, 2, 2, 1, 3, 2, 3, 2, 2, 3, 2, 3, 3, 3, 3, 2, 4, 3, 4, 3, 3, objects count: 26 0, 1, 2, 3, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2, 2, 3, 2, 2, 3, 2, 3, 3, 3, 3, 2, 3, 3, 4, 3, 3, objects count: 30 Change: 5, 12, 12,