The 16th Heilongjiang Provincial Collegiate Programming Contest D. Doin' Time
题意 :
一个数列, 每次合并
题解 : 利用乘法逆元(由快速幂求得逆元)进行类似前缀和的操作, 枚举长度与L和R, 以此来完成DP。
代码 :
#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;
}