搞个前缀和随便DP一下
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 101
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
int n, m, ans1 = ~(1 << 31), ans2;
int a[N], sum[N], f1[N][N][N], f2[N][N][N];
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
int main()
{
int i, j, k, l;
n = read();
m = read();
for(i = 1; i <= n; i++) a[i] = read(), a[i + n] = a[i];
for(i = 1; i < 2 * n; i++) sum[i] = a[i] + sum[i - 1];
memset(f1, 127 / 3, sizeof(f1));
for(k = 1; k <= m; k++)
for(i = 1; i < 2 * n; i++)
for(j = i + k - 1; j < 2 * n; j++)
{
if(k == 1)
{
f1[i][j][k] = f2[i][j][k] = ((sum[j] - sum[i - 1]) % 10 + 10) % 10;
continue;
}
for(l = i + k - 1; l < j; l++)
{
f1[i][j][k] = min(f1[i][j][k], f1[i][l][k - 1] * (((sum[j] - sum[l]) % 10 + 10) % 10));
f2[i][j][k] = max(f2[i][j][k], f2[i][l][k - 1] * (((sum[j] - sum[l]) % 10 + 10) % 10));
}
}
for(i = 1; i <= n; i++)
{
ans1 = min(ans1, f1[i][i + n - 1][m]);
ans2 = max(ans2, f2[i][i + n - 1][m]);
}
printf("%d\n%d\n", ans1, ans2);
return 0;
}