题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571
简单dp,
dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k] )
k 为m的因子
PS:0边界要初始为负数(例如-123456789)越大越好
代码:
#include <stdio.h>
#include <string.h> int dp[25][1005]; #define max(x,y) x > y ? x : y int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
int i,j;
int n,m;
scanf("%d%d",&n,&m);
for(i = 0;i <=m; i++)
dp[0][i] = -123456789;
for(i = 0;i <=n; i++)
dp[i][0] = -123456789;
dp[0][1] = 0;
dp[1][0] = 0; for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
scanf("%d",&dp[i][j]);
int k = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
int dp_ans = max(dp[i-1][j],dp[i][j-1]);
for(k = 1; k < j; k++)
if(j % k == 0)
dp_ans = max(dp_ans,dp[i][k]);
dp[i][j] += dp_ans;
}
printf("%d\n",dp[n][m]);
}
return 0;
}