zoukankan      html  css  js  c++  java
  • bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP

    题目传送门

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

    题解

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

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

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

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


    时间复杂度 (O(130^3log 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
    ", 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;
    }
    
  • 相关阅读:
    路由器DHCP 动态主机配置
    nat转换
    静态路由的实验
    不同vlan之间的相互访问
    IP的包头格式什么?请分析每个字段的含义
    IP地址的分类
    关于对JSON.parse()与JSON.stringify()的理解
    Ajax工作原理
    关于css伪类
    开发 | 如何在微信小程序的页面间传递数据?
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj2004.html
Copyright © 2011-2022 走看看