代码思路就不具体讲述了,和第一篇题解一样
想知道的是 字符串高精度&数组高精度在复杂度方面有什么差异
顺便求优化,正确性应该是没错的,T了最后两个点
#include <bits/stdc++.h>
using namespace std;
string x[87];
string dp[87][87];
inline string add(string a,string b) { // 高精加但是非负
if(a.size()<b.size()){
string temp=a;
a=b;
b=temp;
}
int len1=a.size(),len2=b.size();
a='0'+a;
for (int i=0;i<=len1-len2;++i) b="0"+b;
string sum;
for (int i=0;i<=len1;++i) sum += '0';
int c=0; // 进位
for(int i=len1;i>=0;--i){
int t=(a[i]-'0')+(b[i]-'0')+c;
sum[i]+=t%10;
c=t/10;
}
if(sum[0]=='0') sum=sum.substr(1,sum.size());
return sum;
}
inline string mult(string a,string b) { //高精乘但是非负
if (a.size()<b.size()) {
string temp=a;
a=b;
b=temp;
}
int len1=a.size(),len2=b.size();
string product="0";
for (int i=len2-1;i>=0;--i) {
if (i<len2-1) a+="0";
string temp=a;
if (b[i]=='0') temp="0";
else{
for(int k=1;k<b[i]-'0';++k) temp = add(temp, a);
}
product=add(product,temp);
}
return product;
}
inline bool str_cmp(string a,string b){ // 非负,且a>=b则返回 1
if(a==b) return 1;
else if(a.size()!=b.size()) return a.size()>b.size();
else return a>b;
}
inline string str_max(string a,string b){
if(str_cmp(a,b)) return a;
else return b;
}
string mul[82]={"1","2","4","8","16","32","64","128","256","512","1024","2048","4096","8192","16384","32768","65536","131072","262144","524288","1048576","2097152","4194304","8388608","16777216","33554432","67108864","134217728","268435456","536870912","1073741824","2147483648","4294967296","8589934592","17179869184","34359738368","68719476736","137438953472","274877906944","549755813888","1099511627776","2199023255552","4398046511104","8796093022208","17592186044416","35184372088832","70368744177664","140737488355328","281474976710656","562949953421312","1125899906842624","2251799813685248","4503599627370496","9007199254740992","18014398509481984","36028797018963968","72057594037927936","144115188075855872","288230376151711744","576460752303423488","1152921504606846976","2305843009213693952","4611686018427387904","9223372036854775808","18446744073709551616","36893488147419103232","73786976294838206464","147573952589676412928","295147905179352825856","590295810358705651712","1180591620717411303424","2361183241434822606848","4722366482869645213696","9444732965739290427392","18889465931478580854784","37778931862957161709568","75557863725914323419136","151115727451828646838272","302231454903657293676544","604462909807314587353088","1208925819614629174706176"};
string ans="0",maxx;
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
while(n--){
for(int i=1;i<=80;++i) for(int j=1;j<=80;++j) dp[i][j]="0";
maxx="0";
for(int i=1;i<=m;++i) cin>>x[i];
for(int i=1;i<=m;++i){
for(int j=m;j>=i;--j){
dp[i][j]=str_max( dp[i][j] , add(dp[i-1][j],mult(x[i-1],mul[m-j+i-1])) );
dp[i][j]=str_max( dp[i][j] , add(dp[i][j+1],mult(x[j+1],mul[m-j+i-1])) );
}
}
for(int i=1;i<=m;++i){
maxx=str_max( maxx,add( dp[i][i],mult(x[i],mul[m]) ) );
}
ans=add(ans,maxx);
}
cout<<ans<<"\n";
return 0;
}