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;
}
时间: 2024-10-06 13:20:19

ECNA2016F - Removal Game的相关文章

《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果

本文完全转载:http://www.cnblogs.com/Imageshop/p/3281703.html,再次仅当学习交流使用.. <Single Image Haze Removal Using Dark Channel Prior>一文中图像去雾算法的原理.实现.效果(速度可实时) 本文算法合作联系QQ: 33184777, 非诚勿扰 邮件地址:   [email protected] 最新的效果见 :http://video.sina.com.cn/v/b/124538950-125

Cloud processing (basic) - Conditional removal

Conditional removal Another way of getting the exact same result than with the previous example would be to use a conditional removal filter. We can build any kind of condition we want for the values of the point: #include <pcl/io/pcd_io.h> #include

PCIE AER and hot removal

It is very usefull to disable/enable during our verification of some functions, such as PCIE hotplug, firmware upgrade. We need use AER to maks or enable PCIE errors to stop unexpected reset. Following script give us an example how to control AER and

OpenCASCADE Hidden Line Removal

OpenCASCADE Hidden Line Removal [email protected] Abstract. To provide the precision required in industrial design, drawings need to offer the possibility of removing lines, which are hidden in a given projection. OpenCASCADE provides two algorithms

Forcing the Removal of a Domain Controller

Forcing the Removal of a Domain Controller Reference link https://technet.microsoft.com/en-us/library/cc781245%28v=ws.10%29.aspx Forced removal of a domain controller from Active Directory is intended to be used as a last resort to avoid having to re

论文阅读计划2(Deep Joint Rain Detection and Removal from a Single Image)

Deep Joint Rain Detection and Removal from a Single Image[1] 简介:多任务全卷积从单张图片中去除雨迹.本文在现有的模型上,开发了一种多任务深度学习框架,学习了三个方面,包括二元雨条纹映射(binary rain streak map),雨条纹外观和干净的背景.特别是新添加的二元雨条纹映射,其损失函数可以为神经网络提供额外的强特征.对于雨带积累现象(暴雨形成的如烟如雾的现象),采取循环雨检测和清除,以迭代和渐进方式清除. 动机:恢复暴雨下

异步复位中的recovery time和removal time

什么是recovery time和removal time? 在同步电路中,输入数据需要与时钟满足setup time和hold time才能进行数据的正常传输,防止亚稳态.同样的道理,对于一个异步复位寄存器来说,同样异步复位信号同样需要和时钟满足recovery time和removal time 才能有效进行复位操作和复位释放操作,防止输出亚稳态. 1.recovery time:恢复时间 撤销复位时,恢复到非复位状态的电平必须在时钟有效沿来临之前的一段时间到来,才能保证时钟能有效恢复到非复

POJ 3183 Stump Removal(简单题)

[题意简述]:就是这个树桩,当它比它身边的树桩都高的时候,他就能炸掉身边的树桩.现在让我们使用最少的炸药将所有树桩都炸掉,问这些炸弹都放在哪些树桩上. [分析]:简单的模拟一下,运用贪心法则,只要这个树桩比身边的其他树桩高,就输出它的位置即可. 但是,的确要注意一下边界的处理! //412K 516Ms #include<iostream> using namespace std; int Stump[50005]; int main() { int N; cin>>N; for(

POJ 3183:Stump Removal(模拟)

http://poj.org/problem?id=3183 题意:有n个树桩,分别有一个高度h[i],要用Bomb把树桩都炸掉,如果炸的位置的两边树桩高度小于Bomb炸的树桩高度,那么小于树桩高度的两侧都是可以被炸掉的.而且有传递性.求把树桩全部炸掉要消耗的最少的Bomb数所炸的位置. 看样例: 1 2 5 4 3 3 6 6 2 炸位置3(即5)的话,那么2小于5,所以位置2被炸掉,因为1小于2,所以位置1被炸掉,因为4小于5,所以位于4被炸掉,因为3小于4,所以位置5被炸掉.因此丢一个Bo