分析
确定dp含义
dp[ i ] [ j
]表示第i条街在建了j栋房子情况下的方案数量
状态转移方程
\(dp[i][j + q] = \sum_{q = i - 1}^{k}dp[i -
1][q],其中j从1到m\)
最终状态
dp[ n ][ k ]
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import java.util.Scanner;
public class Main { static int p = (int)1e9 + 7; static int MAXN = 55; static int MAXK = 2605; static long[][] dp = new long[MAXN][MAXK]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int k = scan.nextInt(); for(int i = 0; i <= k; i++) { dp[0][i] = 1; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { for(int q = i - 1; q <= k; q++) { dp[i][j + q] += dp[i - 1][q]; dp[i][j + q] %= p; } } } System.out.println(dp[n][k]); scan.close(); } }
|
问题
MAXN, MAXK的设置
三重for循环顺序的设置
初始化dp[0]这一行