hdu 5471 Count the Grid(容斥)

题目链接:hdu 5471 Count the Grid

解题思路

比赛的时候想太复杂了,因为n只有10,直接搞个容斥就可以解决了。
首先将矩形切割成若干不相交的小矩形,即同一小矩形中的点状态一致,然后判断每个小矩形中v的最大值能是多少,接着考虑单一v值的情况下,用容斥求出答案。复杂度为o(1024*1024)

代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 1024 + 5;
const int mod = 1e9 + 7;

struct Rectangle {
    int x1, y1, x2, y2, c;
    Rectangle (int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0, int c = 0): x1(x1), y1(y1), x2(x2), y2(y2), c(c) {}
    void read() { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c); }
    int size() { return (x2-x1+1)*(y2-y1+1); }
    bool in(const Rectangle& u) const {
        return x1 <= u.x1 && u.x2 <= x2 && y1 <= u.y1 && u.y2 <= y2;
    }
}R[15];

int N, M, H, W, cc, all, ct[15][maxn], sz[15][maxn];
vector<int> X, Y, C, G[15];

int bitcount(int x) { return x == 0 ? 0 : bitcount(x>>1) + (x&1); }

void init () {
    scanf("%d%d%d%d", &H, &W, &M, &N);
    X.clear(), Y.clear(), C.clear();

    for (int i = 0; i < N; i++) {
        R[i].read();
        X.push_back(R[i].x1-1);
        X.push_back(R[i].x2);
        Y.push_back(R[i].y1-1);
        Y.push_back(R[i].y2);
        C.push_back(R[i].c);
    }
    X.push_back(0);
    X.push_back(H);
    Y.push_back(0);
    Y.push_back(W);

    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    sort(C.begin(), C.end());
    int cx = unique(X.begin(), X.end()) - X.begin();
    int cy = unique(Y.begin(), Y.end()) - Y.begin();
    cc = unique(C.begin(), C.end()) - C.begin();

    int id[15], rk[15];
    for (int i = 0; i < cc; i++) G[i].clear();
    for (int i = 0; i < N; i++) {
        id[i] = lower_bound(C.begin(), C.begin()+cc, R[i].c) - C.begin();
        rk[i] = G[id[i]].size();
        G[id[i]].push_back(i);
    }

    all = 0;
    memset(ct, 0, sizeof(ct));
    memset(sz, 0, sizeof(sz));
    for (int x = 1; x < cx; x++) {
        for (int y = 1; y < cy; y++) {
            int t = cc, s = 0;
            Rectangle tmp = Rectangle(X[x-1]+1, Y[y-1]+1, X[x], Y[y]);
            for (int i = 0; i < N; i++) {
                if (R[i].in(tmp)) {
                    if (id[i] < t)
                        s = 0, t = id[i];
                    if (t == id[i])
                        s |= (1<<rk[i]);
                }
            }
            if (t == cc) all += tmp.size();
            else {
                int add = tmp.size();
                for (int j = s; j; j = (j-1)&s)
                    ct[t][j] += add;
            }
        }
    }

    for (int c = 0; c < cc; c++) {
        int s = (1<<G[c].size());
        for (int i = 0; i < s; i++) {
            for (int j = i; j; j = (j-1)&i)
                sz[c][i] += ct[c][j] * (bitcount(j)&1 ? 1 : -1);
        }
    }
}

ll pow_mod(ll x, ll n) {
    ll ret = 1;
    while (n) {
        if (n&1) ret = ret * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return ret;
}

int solve () {
    ll ret = pow_mod(M, all);
    for (int i = 0; i < cc; i++) {
        int t = 0, n = (1<<G[i].size());
        for (int s = 0; s < n; s++) {
            int si = bitcount(s)&1 ? -1 : 1;
            t = ((t + si * pow_mod(C[i]-1, sz[i][s]) * pow_mod(C[i], sz[i][n-1]-sz[i][s])) % mod + mod) % mod;
        }
        ret = ret * t % mod;
    }
    return ret;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        init();
        printf("Case #%d: %d\n", kcas, solve());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值