跳到正文

个人记录

The 16th Heilongjiang Provincial Collegiate Programming Contest D. Doin' Time

LiunxSW约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年1月13日
更新
2026年2月3日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

题意 : 一个数列, 每次合并 axa_x 与 ax+1a_{x+1} 为 axa_x * ax+1a_{x + 1}, 得到 (axa_x − ax−1a_{x -1})2^2 的贡献, 问如何使贡献最大化。

题解 : 利用乘法逆元(由快速幂求得逆元)进行类似前缀和的操作, 枚举长度与L和R, 以此来完成DP。

代码 :

C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define _for(i, a, b) for(int i = a; i < b; i++)
#define _rep(i, a, b) for(int i = a; i <= b; i++)
#define x first
#define y second
using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int MAXN = 3e2 + 10, MOD = 1e6 + 3;

LL a[MAXN], p[MAXN], q[MAXN], d[MAXN][MAXN], n;

LL qmi(int a, int b) {
    LL res = 1 % MOD;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * (LL) a % MOD;
        b >>= 1;
    }
    return res;
}

int main() {
    cin >> n;
    p[0] = q[0] = 1;
    _rep(i, 1, n) cin >> a[i];
    _rep(i, 1, n) {
        p[i] = a[i] * p[i - 1] % MOD;
        q[i] = qmi(p[i], MOD - 2);
    }
    _rep(len, 2, n) _rep(L, 1, n - len + 1) {
        int R = L + len - 1;
        _for(k, L, R) {
            LL x = p[k] * q[L - 1] % MOD, y = p[R] * q[k] % MOD;
            d[L][R] = max(d[L][R], d[L][k] + d[k + 1][R] + (x - y) * (x - y));
        }
    }
    cout << d[1][n];
    return 0;
}
原文