前言

怀疑昨天晚上考试的时候没带脑子...

\(\sf Description\)

\(\text{Link.}\)

\(\sf Solution\)

这道题可以直接硬搜 \(\rm qwq\)。乍一看好像是 \(\mathcal O(qm!)\) 的,不过事实果真如此吗?

我们注意到一个很重要的条件:\(a_i\) 是单调不减的,且均在 \([1,m]\) 之间。我们可以将 \(a_i\) 的种类数转化为在长度为 \(n\) 的序列上划分 \(m\) 个段(同一个段代表相同的数),段内可以为空(可以有数不被选)。所以种类数为 \(\binom{n+m-1}{m-1}\)

总复杂度实际上是 \(\mathcal O\left (\binom{19}{9}\cdot q\right )\)

\(\sf Code\)

#include <cstdio>
#include <iostream>
using namespace std;

int n, m, q, a[55], b[55], c[55], d[55], A[15], ans;

int read() {
    int x = 0, f = 1; char s;
    while((s = getchar()) < '0' || s > '9') if(s == '-') f = -1;
    while(s >= '0' && s <= '9') {x = (x << 1) + (x << 3) + (s ^ 48); s = getchar();}
    return x * f;
}

void dfs(const int now, const int lim) {
    if(now == n + 1) {
        int res = 0;
        for(int i = 1; i <= q; ++ i)
            if(A[b[i]] - A[a[i]] == c[i]) res += d[i];
        ans = max(ans, res);
        return;
    }
    for(int i = lim; i <= m; ++ i)
        A[now] = i, dfs(now + 1, i);
}

int main() {
    n = read(), m = read(), q = read();
    for(int i = 1; i <= q; ++ i) a[i] = read(), b[i] = read(), c[i] = read(), d[i] = read();
    dfs(1, 1);
    printf("%d\n", ans);
    return 0;
}
posted on 2020-05-03 11:19  Oxide  阅读(171)  评论(1编辑  收藏  举报