2种方法
2023-03-22 08:08:56
发布于:上海
25阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int N = 100, M = 20;
int n, m;
long long dp[N][M], a[N][N];
string num, f[N][M], b[N][N];
string max (string s1, string s2) {
int n1 = s1.size(), n2 = s2.size();
if (n1 != n2) return n1>n2?s1:s2;
for (int i = 0;i < n1;i ++)
if (s1[i] != s2[i])
return s1[i] > s2[i] ? s1 : s2;
return s1;
}
string add (string s, int c) {
int nn = s.size();
for (int i = nn-1;i >= 0;i --) {
int t = s[i] - '0';
s[i] = (t + c) % 10 + '0';
c = (t + c) / 10;
}
return (c == 0 ? s : "1" + s);
}
string mul (string s1, string s2) {
int len1 = s1.size(), len2 = s2.size();
int c[len1+len2+1], a[len1+1], b[len2+1];
s1 = '.'+s1; s2 = '.'+s2;
memset(c, 0, sizeof c);
for(int i=1;i<=len1;i++)
a[len1-i+1]=s1[i]-'0';
for(int i=1;i<=len2;i++)
b[len2-i+1]=s2[i]-'0';
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++){
c[i+j-1]+=a[i]*b[j];
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
}
int len=len1+len2-1;
if(c[len+1]!=0) len++;
string res = "";
for(int i=len;i>=1;i--)
res += to_string(c[i]);
return res;
}
string mul(string s){
return s+"0";
}
int main(){
cin >> n >> m >> num;
for (int i = 0;i < n;i ++)
for (int j = i;j < n;j ++)
b[i+1][j+1] = add(mul(b[i+1][j]), num[j]-'0');
for (int i = 1;i <= n;i ++)
f[i][1] = b[1][i];
for (int i = 1;i <= n;i ++)
for (int j = 2;j <= m+1 && j <= i;j ++)
for (int k = j;k <= i;k ++)
f[i][j] = max(mul(f[k-1][j-1], b[k][i]), f[i][j]);
cout << f[n][m+1] << endl;
return 0;
}
这里空空如也
有帮助,赞一个