Hdu 2571 fate

Hdu 2571 Destiny (dp)

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=2571
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
problem-solving ideas: each state will step down inherit from multiple states, each taking the top or left of their own factor maximum bit then add their own the value gradually updated on the line;
the following code:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int num[25][1005],dp[25][1005];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof(dp));
		int n,m;
		scanf("%d %d",&n,&m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&num[i][j]);
				dp[i][j]=-inf; //初始化
			}
		}
		for(int i=0;i<n;i++)
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(i==1&&j==1){
					dp[i][j]=num[i][j];
					continue;
				}
				if(i==1)dp[i][j]=dp[i][j-1];//在最上一行时只能从左边继承
				else if(j==1)dp[i][j]=dp[i-1][j];//在最左一列时只能从上面继承
				else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//不然就都能继承
				for(int k=j-1;k>0;k--){
					if(j%k==0){//找j的因子,j位能从所有自己因子位的数继承
						dp[i][j]=max(dp[i][j],dp[i][k]);
					}
				}
				dp[i][j]+=num[i][j];//加上自己位的值
			}
		}
		printf("%d\n",dp[n][m]);
	}
	return 0;
} 
Published 35 original articles · won praise 3 · Views 873

Guess you like

Origin blog.csdn.net/weixin_43823753/article/details/104947508