完全背包类似的问题,f表示状态,f[j]表示数字j能否被凑出来,做法是减去现有的笼子数,往下递减看更小的数能否被凑出来。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N = 100 * 99 - 100 - 99;
static int v[];
static boolean f[];
static void init() {
f = new boolean[N + 1];
v = new int[101];
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static void main(String[] args) throws IOException {
init();
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
v[i] = Integer.parseInt(br.readLine());
f[v[i]] = true;
}
int c = v[0];
for (int i = 1; i < n; i++) c = gcd(c, v[i]);
if (c == 1) {
for (int i = 0; i < n; i++) {
for (int j = v[i]; j <= N; j++)
f[j] = f[j] || f[j - v[i]];
}
int res = 0;
for (int i = 1; i <= N; i++) {
if (!f[i]) res++;
}
System.out.println(res);
} else System.out.println("INF");
}
}