bzoj2004(矩阵快速幂,状压DP)
每个长度为p的区间都必须出现k次1,数据又很小,我们使用状压。
dp[i][j]->dp[i+1][j'],dp[i][j]表示当前考虑到了第i个车站,包括第i个其后的p个的状态(有车停或没车停),其中j'为j中某一个车动一次到达的状态,为了防止复杂度爆炸,先dfs求一遍有用的状态。
然后矩阵转移。。
另外提一下初末状态,注意到起始站的限制,初末状态都应是p个里前k个都是1。(在代码实现中就正好对应了第一个dfs的状态)
这也是开始B.t[1][1]赋值1和最后输出B.t[1][1]的原因
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=,mod=;
struct matr{
int t[maxn][maxn],n,m;
matr operator*(matr &a){
matr c;
for(int i=;i<=n;++i)
for(int j=;j<=a.m;++j){
c.t[i][j]=;
for(int k=;k<=n;++k)
c.t[i][j]=(c.t[i][j]+t[i][k]*a.t[k][j])%mod;
}
c.n=n;c.m=a.m;
return c;
}
}A,B;
int n,k,p,q[maxn];
void dfs(int pos,int num,int cnt){
if(cnt==k){q[++q[]]=num;return;}
for(int i=pos-;i>=;--i)
dfs(i,num+(<<i),cnt+);
}
int check(int x,int y){
int tmp=x-(<<(p-));
tmp<<=;tmp^=y;
if(tmp==(tmp&(-tmp)))return ;
return ;
}
int main(){
cin>>n>>k>>p;
dfs(p-,(<<(p-)),);
A.n=A.m=B.n=B.m=q[];
for(int i=;i<=q[];++i)B.t[i][i]=;
for(int i=;i<=q[];++i)
for(int j=;j<=q[];++j){
if(check(q[i],q[j]))A.t[i][j]=;
}
/*for(int i=1;i<=q[0];++i){
for(int j=1;j<=q[0];++j)
cout<<A.t[i][j]<<' ';
cout<<endl;
}*/
int cc=n-k;
while(cc){
if(cc&)B=B*A;
cc>>=;A=A*A;
}
cout<<B.t[][]<<endl;
//system("pause");
return ;
}
bzoj2004(矩阵快速幂,状压DP)的更多相关文章
- bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2004 题解 如果 \(N\) 没有那么大,考虑把每一位分配给每一辆车. 假设已经分配到了第 \ ...
- hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)
题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...
- Codeforces Round #554 (Div. 2) F2. Neko Rules the Catniverse (Large Version) (矩阵快速幂 状压DP)
题意 有nnn个点,每个点只能走到编号在[1,min(n+m,1)][1,min(n+m,1)][1,min(n+m,1)]范围内的点.求路径长度恰好为kkk的简单路径(一个点最多走一次)数. 1≤n ...
- bzoj2004 矩阵快速幂优化状压dp
https://www.lydsy.com/JudgeOnline/problem.php?id=2004 以前只会状压dp和矩阵快速幂dp,没想到一道题还能组合起来一起用,算法竞赛真是奥妙重重 小Z ...
- POJ 3744 【矩阵快速幂优化 概率DP】
搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...
- 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]
矩阵填数 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- 一本通 1783 矩阵填数 状压dp 容斥 计数
LINK:矩阵填数 刚看到题目的时候感觉是无从下手的. 可以看到有n<=2的点 两个矩形. 如果只有一个矩形 矩形外的方案数容易计算考虑 矩形内的 必须要存在x这个最大值 且所有值<=x. ...
- hdu 5564 Clarke and digits 矩阵快速幂优化数位dp
Clarke and digits Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- Web.config Transformation Syntax for Web Application Project Deployment
Web.config Transformation Syntax for Web Application Project Deployment Other Versions Updated: Ma ...
- 从1~N中任选出三个数,最小公倍数最大
已知一个正整数N,问从1~N中任选出三个数,它们的最小公倍数最大可以为多少. 当n为奇数:n.n-1.n-2这是三个最大数,并且它们两两互质.因为连续的奇.偶.奇,互质.连续的两个数互质是因为它们的公 ...
- python 打开文件对话框 filedialog tkinter GUI 编程
- -读取文件的gui编程 # _*_ coding:utf-8 _*_ import tkinter from tkinter import filedialog def openfiles2(): ...
- Python generator 的yield (enumerate)
生成杨辉三角 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 def triangles(max): L = [1,] while len(L) - 1 < ...
- 数据结构:链表 >> 链表按结点中第j个数据属性排序(冒泡排序法)
创建结点类,链表类,测试类 import java.lang.Object; //结点node=数据date+指针pointer public class Node { Object iprop; p ...
- 2017/2/10:Manven简介与项目管理(入门)
1.Maven工程的创建 2.使用Manven manven配置文件主要集中在 http://m.blog.csdn.net/article/details?id=50316383
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
引言 前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿.现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做.由于C#中通过直接添加引用的方来调用 ...
- c# 通过按钮获取文件夹和打开磁盘文件
Button控件获取文件夹: FolderBrowserDialog fileDialog = new FolderBrowserDialog(); if (fileDialog.ShowDialog ...
- spring学习九 spring aop详解
本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...
- 155. Min Stack - Unsolved
https://leetcode.com/problems/min-stack/#/solutions Design a stack that supports push, pop, top, and ...