hdu2571命运 【dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2571
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; #define N 25 #define M 1010 #define inf 0x3f3f3f3f int dp[N][M],map[N][M]; int main() { int t,m,n,i,j,k; scanf("%d",&t); while(t --) { scanf("%d%d",&n,&m); for(i = 1; i <= n; i ++) for(j = 1; j <= m; j ++) scanf("%d",&map[i][j]); for(i = 0; i <= n; i ++) dp[i][0] = -inf; for(i = 0; i <= m; i ++) dp[0][i] = -inf; dp[0][1] = dp[1][0] = 0; for(i = 1; i <= n; i ++) { for(j = 1; j <= m; j ++) { dp[i][j] = max(dp[i-1][j],dp[i][j-1]); for(k = 2; k <= m; k ++) { if(k > j) break; if(j%k == 0) dp[i][j] = max(dp[i][j],dp[i][j/k]); } dp[i][j] += map[i][j]; } } printf("%d\n",dp[n][m]); } return 0; }