凸包处理后,枚举注意切开后至少是个三角形所以R-L> 2
#include <ctime>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof a)
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define scd(num) scanf("%lf",&num)
typedef long long LL;
const int MAX_N = 310;
const double EPS = 1e-10;
LL N,MOD;
LL cost[MAX_N][MAX_N];
LL add(LL a,LL b)
{
return a + b;
}
struct P
{
LL x,y;
P() {}
P(double x,double y) : x(x),y(y){
}
P operator + (P p)
{
return P(add(x,p.x),add(y,p.y));
}
P operator - (P p)
{
return P(add(x,-p.x),add(y,-p.y));
}
P operator * (double d)
{
return P(x * d,y * d);
}
LL dot (P p)
{
return add(x * p.x ,y * p.y);
}
LL det(P p)
{
return add(x * p.y , -y * p.x);
}
} points[MAX_N];
vector<P> convex;
bool cmp_x(const P& p,const P& q)
{
if (p.x != q.x) return p.x < q.x;
return p.y < q.y;
}
vector<P> convex_hull(P* ps,int n)
{
sort(ps,ps + N,cmp_x);
int k = 0;
vector<P> qs(n * 2);
for (int i = 0;i < n;i++)
{
while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) k--;
qs[k++] = ps[i];
}
for (int i = n - 2,t = k;i >= 0;i--)
{
while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) k--;
qs[k++] = ps[i];
}
qs.resize( k - 1);
return qs;
}
LL dp[MAX_N][MAX_N];
LL cal(int l,int r) {
if (dp[l][r] != -1 )return dp[l][r];
if (r - l <= 2) return 0;
dp[l][r] = 1LL<<60;
for (int i = l+1;i < r;i++) {
dp[l][r] = min(dp[l][r],cal(l,i) +cal(i,r) + cost[l][i] + cost[i][r]);
}
return dp[l][r];
}
int main()
{
while (~scanf("%lld%lld",&N,&MOD)) {
for (int i = 0;i < N;i++) {
scl(points[i].x); scl(points[i].y);
}
convex = convex_hull(points,N);
int nc = convex.size();
if (nc != N) {
printf("I can't cut.\n");
continue;
}
mem(cost,0);
mem(dp,-1);
for (int i = 0;i < nc;i++) {
for (int j = i + 2;j < nc;j++) {
P plus = convex[i] + convex[j];
cost[i][j] = cost[j][i] = abs((LL)plus.x) * abs((LL)plus.y) % MOD;
}
}
printf("%lld\n",cal(0,N - 1));
}
return 0;
}