1069. 凸多边形的划分
作者:
qbz666
,
2021-11-08 20:39:15
,
所有人可见
,
阅读 209
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
typedef long long LL;
int n;
int w[N];
vector<int>f[N][N];
bool cmp(vector<int>& a, vector<int>& b)
{
if (a.size() != b.size())return a.size() < b.size();
for (int i = a.size() - 1; i >= 0; i--)
{
if (a[i] != b[i])
return a[i] < b[i];
}
return true;
}
vector<int>add(vector<int>a, vector<int>b)
{
vector<int>c;
int t = 0;
for (int i = 0; i < (int)a.size() || i < (int)b.size(); i++)
{
if (i < (int)a.size())t += a[i];
if (i < (int)b.size())t += b[i];
c.push_back(t % 10);
t /= 10;
}
while (t)
{
c.push_back(t % 10);
t /= 10;
}
return c;
}
vector<int>mul(vector<int>a, LL b)
{
vector<int>c;
LL t = 0;
for (int i = 0; i < (int)a.size(); i++)
{
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (t)
{
c.push_back(t % 10);
t /= 10;
}
return c;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)cin >> w[i];
for (int len = 3; len <= n; len++)
{
for (int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
for (int k = l + 1; k < r; k++)
{
auto val = mul(mul({ w[l] }, w[k]), w[r]);
val = add(add(val, f[l][k]), f[k][r]);
if (f[l][r].empty() || cmp(val, f[l][r]))f[l][r] = val;
}
}
}
auto res = f[1][n];
for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];
cout << endl;
return 0;
}