ECNA2016F - Removal Game
题目大意
给你$n\le 100$个数,每次消去一个数的代价是相邻两个数的gcd(循环意义下),最后剩下两个数再取gcd作为代价,问最后消到只剩两个数的代价和最小是多少。
简要题解
dp就好,设f[i][j]表示消去$[i+1,j-1]$里所有数的代价,枚举中间元转移就好,这是区间dp的一般套路嘛,注意取答案的时候只考虑消去n-2个元素,区间长度为n-1,默认最右边的元素留下在加上内部还有一个元素剩下。我真是越学越回去了,天天做煞笔题qaq,不对,哪里天天了。。。
#include <bits/stdc++.h> using namespace std; namespace my_header { #define pb push_back #define mp make_pair #define pir pair<int, int> #define vec vector<int> #define pc putchar #define clr(t) memset(t, 0, sizeof t) #define pse(t, v) memset(t, v, sizeof t) #define bl puts("") #define wn(x) wr(x), bl #define ws(x) wr(x), pc(' ') const int INF = 0x3f3f3f3f; typedef long long LL; typedef double DB; inline char gchar() { char ret = getchar(); for(; (ret == '\n' || ret == '\r' || ret == ' ') && ret != EOF; ret = getchar()); return ret; } template<class T> inline void fr(T &ret, char c = ' ', int flg = 1) { for(c = getchar(); (c < '0' || '9' < c) && c != '-'; c = getchar()); if (c == '-') { flg = -1; c = getchar(); } for(ret = 0; '0' <= c && c <= '9'; c = getchar()) ret = ret * 10 + c - '0'; ret = ret * flg; } inline int fr() { int t; fr(t); return t; } template<class T> inline void fr(T&a, T&b) { fr(a), fr(b); } template<class T> inline void fr(T&a, T&b, T&c) { fr(a), fr(b), fr(c); } template<class T> inline char wr(T a, int b = 10, bool p = 1) { return a < 0 ? pc('-'), wr(-a, b, 0) : (a == 0 ? (p ? pc('0') : p) : (wr(a/b, b, 0), pc('0' + a % b))); } template<class T> inline void wt(T a) { wn(a); } template<class T> inline void wt(T a, T b) { ws(a), wn(b); } template<class T> inline void wt(T a, T b, T c) { ws(a), ws(b), wn(c); } template<class T> inline void wt(T a, T b, T c, T d) { ws(a), ws(b), ws(c), wn(d); } template<class T> inline T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template<class T> inline T fpw(T b, T i, T _m, T r = 1) { for(; i; i >>= 1, b = b * b % _m) if(i & 1) r = r * b % _m; return r; } }; using namespace my_header; const int MAXN = 111 * 2; int n, a[MAXN], g[MAXN][MAXN], f[MAXN][MAXN]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { #ifdef lol freopen("F.in", "r", stdin); freopen("F.out", "w", stdout); #endif while (scanf("%d", &n) != EOF && n) { for (int i = 1; i <= n; ++i) { a[i] = a[i + n] = fr(); } n = n << 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) g[i][j] = gcd(a[i], a[j]); } int ans = INF; memset(f, INF, sizeof f); for (int i = 1; i <= n; ++i) f[i][i + 1] = 0; for (int i = 1; i <= n / 2; ++i) { for (int j = 1; j + i + 1 <= n; ++j) { int k = i + j + 1; for (int l = j + 1; l < k; ++l) f[j][k] = min(f[j][l] + f[l][k] + g[j][l] + g[l][k], f[j][k]); } } for (int i = 1; i <= n / 2; ++i) { int j = i + n / 2; for (int k = i; k <= j; ++k) ans = min(ans, f[i][k] + f[k][j] + g[i][k] + g[k][j] + g[k][j]); } for (int i = 1; i <= n / 2; ++i) ans -= g[i][i + 1]; wt(ans); } return 0; }