bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2004

题解

如果 \(N\) 没有那么大,考虑把每一位分配给每一辆车。

假设已经分配到了第 \(i\) 位,那么想要知道合不合法,我们需要知道每一辆车的上一个停靠点距离现在有多少的距离。考虑直接状压这个东西,发现它的数据量为 \(P_{p}^k\),很大。

但是我们可以发现,每一个位置到底是哪辆车无关紧要,我们只需要知道每个位置有没有车就可以了。于是数据量降低为 \(\binom p{\frac p2}\)。另外,\(0\) 这个距离是必须要选的,这样也可以剪枝掉一些。

于是大概最终的状态数为 \(130\) 左右,可以使用矩阵快速幂来加速。


时间复杂度 \(O(130^3\log n)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

#define lowbit(x) ((x) & -(x))

const int N = 130;
const int NP = (1 << 10) + 7;
const int P = 30031;

int n, p, k, cnt, S, T;
int st[N], s1[NP], mp[NP];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    return ans;
}

struct Matrix {
    int a[N][N];
    
    inline Matrix() { memset(a, 0, sizeof(a)); }
    inline Matrix(const int &x) {
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= cnt; ++i) a[i][i] = x;
    }
    
    inline Matrix operator * (const Matrix &b) {
        Matrix c;
        for (int k = 1; k <= cnt; ++k)
            for (int i = 1; i <= cnt; ++i)
                for (int j = 1; j <= cnt; ++j)
                    sadd(c.a[i][j], a[i][k] * b.a[k][j] % P);
        return c;
    }
} A, B;

inline Matrix fpow(Matrix x, int y) {
    Matrix ans(1);
    for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
    return ans;
}

inline void ycl() {
    S = (1 << p) - 1;
    for (int s = 0; s <= S; ++s) {
        if (s) s1[s] = s1[s ^ lowbit(s)] + 1;
        if (s1[s] == k && (s & 1)) st[++cnt] = s, mp[s] = cnt;
    }
    T = mp[(1 << k) - 1];
}

inline void ycl2() {
    for (int i = 1; i <= cnt; ++i) {
        int s = st[i];
        if ((s >> (p - 1)) & 1) A.a[i][mp[((s << 1) & S) | 1]] = 1;
        else for (int j = 0; j < p; ++j) if ((s >> j) & 1) A.a[i][mp[((s ^ (1 << j)) << 1) | 1]] = 1;
    }
}

inline void work() {
    ycl();
    ycl2();
    B.a[T][1] = 1;
    B = fpow(A, n - k) * B;
    printf("%d\n", B.a[T][1]);
}

inline void init() {
    read(n), read(k), read(p);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/hankeke/p/bzoj2004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值