AcWing 1069. 凸多边形的划分(__int128偷懒)
原题链接
中等
#include<bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define clr(arr,a) memset(arr, a, sizeof(arr))
#define IOS ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, int> Pll;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MOD = 11380;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2+10;
__int128 dp[maxn][maxn];
ll a[maxn];
int main(void) {
int n; cin >> n;
for (int i = 1; i<=n; ++i) cin >> a[i];
__int128 t = 1;
for (int i = 1; i<=n; ++i)
for (int j = 1; j<=n; ++j)
dp[i][j] = 1e30;
for (int i = 1; i<=n-2; ++i) dp[i][i+2] = t*a[i]*a[i+1]*a[i+2];
for (int i = 1; i<=n-1; ++i) dp[i][i+1] = 0;
for (int len = 3; len<n; ++len)
for (int l = 1; l+len<=n; ++l) {
int r = l+len;
//下面k取l+1和r-1实际上是划分了两个区间,那个长度为2的区间的贡献应该为0,所以上面dp[i][i+1] = 0
for (int k = l+1; k<r; ++k) dp[l][r] = min(dp[l][r], dp[l][k]+dp[k][r]+t*a[l]*a[r]*a[k]);
}
string ans;
while(dp[1][n]) {
ans += dp[1][n]%10+'0';
dp[1][n] /= 10;
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
return 0;
}