DP问题,好久不做dp问题还有点陌生,这个问题是m拆分成n个数相加,有多少个,考虑n中最小值如果是0,那么答案不变,如果不是0,从1开始一个一个分。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N = 11, T, m, n;
static int f[][];
public static void init() {
f = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(f[i], 0);
}
f[0][0] = 1;
}
public static void main(String[] args) throws IOException {
T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
init();
String s[] = br.readLine().split(" ");
m = Integer.parseInt(s[0]);
n = Integer.parseInt(s[1]);
for (int k = 0; k <= m; k++) {
for (int j = 1; j <= n; j++) {
f[k][j] = f[k][j - 1];
if (k >= j) f[k][j] += f[k - j][j];
}
}
System.out.println(f[m][n]);
}
}
}