AcWing 414. 数字游戏
原题链接
简单
作者:
Value
,
2020-09-07 17:02:38
,
所有人可见
,
阅读 277
#include <iostream>
#include <limits.h>
#include <cstring>
using namespace std;
const int N = 110, M = 10;
int a[N], s[N];
int f[N][N][M], g[N][N][M];
int get_mod(int x){
return (x % 10 + 10) % 10;
}
int main(){
int n, m; cin >> n >> m;;
for(int i = 1; i <= n; i ++ ){
cin >> a[i];
a[i + n] = a[i];
}
for(int i = 1; i <= 2 * n; i ++ ) s[i] = s[i - 1] + a[i];
memset(f, 0x3f3f3f, sizeof f);
memset(g, -0x3f3f3f, sizeof g);
for(int len = 1; len <= n; len ++ ){
for(int l = 1; l + len - 1 <= 2 * n; l ++ ){
int r = l + len - 1;
for(int k = 1; k <= m; k ++ ){
if(k == 1) f[l][r][k] = g[l][r][k] = get_mod(s[r] - s[l - 1]);
else{
for(int j = l + k - 2; j < r; j ++ ){
f[l][r][k] = min(f[l][r][k], f[l][j][k - 1] * get_mod(s[r] - s[j]));
g[l][r][k] = max(g[l][r][k], g[l][j][k - 1] * get_mod(s[r] - s[j]));
}
}
}
}
}
int maxv = INT_MIN, minv = INT_MAX;
for(int i = 1; i <= n; i ++ ){
minv = min(minv, f[i][i + n - 1][m]);
maxv = max(maxv, g[i][i + n - 1][m]);
}
cout << minv << endl;
cout << maxv << endl;
return 0;
}