现有n1+n2种面值的硬币,其中前n1种为普通币,可以取任意枚,后n2种为纪念币,每种最多只能取一枚,每种硬币有一个面值,问能用多少种方法拼出m的面值?
约瑟夫环
public class Code_JosephusProblem {
public static class Node {
public int value;
public Node next;
public Node(int data) {
value = data;
}
}
public static Node josephusKill1(Node head, int m) {
if (head == null || head.next == head || m < 1) {
return head;
}
Node last = head;
while (last.next != head) {
last = last.next;
}
int count = 0;
while (head != last) {
if (++count == m) {
last.next = head.next;
count = 0;
} else {
last = last.next;
}
head = last.next;
}
return head;
}
public static Node josephusKill2(Node head, int m) {
if (head == null || head.next == head || m < 1) {
return head;
}
Node cur = head.next;
int tmp = -1;
while (cur != head) {
tmp++;
cur = cur.next;
}
tmp = getLive(tmp, m);
while (--tmp != 0) {
head = head.next;
}
return head;
}
public static int getLive(int i, int m) {
if (i == 1) {
return 1;
}
return (getLive(i - 1, m) + m - 1) % i + 1;
}
public static int live(int n, int[] arr) {
return no(n, arr, 0);
}
public static int no(int i, int[] arr, int index) {
if (i == 1) {
return 1;
}
return (no(i - 1, arr, nextIndex(arr.length, index)) + arr[index] - 1) % i + 1;
}
public static int nextIndex(int size, int index) {
return index == size - 1 ? 0 : index + 1;
}
}