不得不说,动态规划是真的骚
题解已经在图片里面了
代码如下:
#include<stdio.h>
long long gethnum(long long n);int main(void)
{//定义变量并输入int N, M;long long dp[19][7] = {0}, num[20][20] = {0};scanf("%d%d", &N, &M);//开始输入数字串并处理{long long tmp, tmp2 = 1, tmp3;for(int i = 1; i < N; i++)tmp2 *= 10;scanf("%lld", &tmp);//利用tmp填充numfor(int x = 1; x <= N; x++){tmp3 = gethnum(tmp);for(int i = 1; i <= x; i++)for(int j = x; j <= N; j++)num[i][j] = num[i][j] * 10 + tmp3;tmp %= tmp2, tmp2 /= 10;}}//开始动态规划for(int i = 1; i <= N; i++)dp[i][0] = num[1][i];for(int i = 1; i <= N; i++){for(int j = 1; j <= M && j < i; j++){long long maxn = 0;for(int k = 1; k < i; k++){long long tmp = dp[k][j - 1] * num[k + 1][i];maxn = (maxn > tmp) ? maxn : tmp;}dp[i][j] = maxn;}}//输出结果printf("%lld", dp[N][M]);return 0;
}
long long gethnum(long long n)
{while(n > 9)n /= 10;return n;
}//得到n的最高位的数字