题目大意
小Z所在的城市有$N$个公交车站,排列在一条长为$N-1$公里的直线上,从左到右依次编号为$1$到$N$,相邻公交车站间的距离均为$1$公里。
作为公交车线路的规划者,小Z调查了市民的需求,决定按以下规则设计线路:
1.设共有$K$辆公交车,则$1$到$K$号车站作为始发站,$N-K+1$到$N$号车站作为终点站。
2.每个车站必须被一辆且仅一辆公交车经停(始发站和终点站也算被经停)。
3.公交车只能从编号较小的车站驶向编号较大的车站。
4.一辆公交车经停的相邻两个车站间的距离不得超过P公里。
注意“经停”是指经过并停车,因经过不一定会停车,故经停与经过是两个不同的概念。在最终确定线路之前,小Z想知道有多少种满足要求的方案。由于答案可能很大,你只需求出答案对$30031$取模的结果。
题目分析
比较简单的轮廓线Dp。
其中题目给出的$p$即为轮廓线。
设状态$f[i,S]$为最靠左的公交车在$i$位置,当前$p$个位置的覆盖情况为$S$的方案数。

如图,转移的过程即为将最左边的公交车移动到右边任意一个格子。
因此,$f[i,from]\rightarrow f[i+1,to]$转移的条件即为:设$from$左移一位,高位超出的减掉,低位补$0$得到的状态为$from’$,则$from’\,xor\,to$仅包含一个$1$。
但是这样转移状态量偏大,用矩阵是存不下/超时的。
因此我们先对合法状态编号,去掉不合法状态。
$S$是合法状态条件为:最高位为$1$,$S$中包含$k$个$1$。
其中起始状态最高位有$k$个$1$。

结束状态本应是最低位有$k$个$1$,但因为规定了最高位为$1$,故结束状态与起始状态一样,均为最高位有$k$个$1$,后面的$0$已唯一确定。
因为每次都是从$i$转移到$i+1$,故转移关系图可以看做是一个有向图,转移$n-k$次得到答案,故使用矩阵快速幂优化转移即可。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } typedef long long LL; const int maxn=205,mod=30031; struct Matrix { LL n,m,a[maxn][maxn]; Matrix(LL n,LL m) { init(n,m); } Matrix(LL n,LL m,char E) { init(n,m); for(int i=1; i<=n; i++)a[i][i]=1; } void init(LL n,LL m) { this->n=n; this->m=m; memset(a,0,sizeof(a)); } LL* operator [] (const LL x) { return a[x]; } Matrix operator * (Matrix& b) { Matrix c(n,b.m); for(int i=1; i<=n; i++) for(int j=1; j<=b.m; j++) for(int k=1; k<=m; k++) c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod; return c; } void operator *= (Matrix& b) { *this=*this*b; } Matrix operator ^ (LL b) { Matrix ans(n,m,'e'),a=*this; while(b>0) { if(b&1)ans=ans*a; a*=a; b>>=1; } return ans; } }; int Count(int s) { int cnt=0; while(s) { if(s&1)cnt++; s>>=1; } return cnt; } int n,k,p,cnt=0,Start,state[1025]; bool Check(int from,int to) { from=(from<<1)&((1<<p)-1); int tmp=from^to; if(tmp==0)return 0; return tmp==(tmp&(-tmp)); } int main() { n=Get_Int(); k=Get_Int(); p=Get_Int(); for(int i=1<<(p-1); i<(1<<p); i++) if(Count(i)==k) { state[++cnt]=i; if(i==(((1<<k)-1)<<(p-k)))Start=cnt; } Matrix M(cnt,cnt); for(int i=1; i<=cnt; i++) for(int j=1; j<=cnt; j++) if(Check(state[i],state[j]))M[i][j]=1; M=M^(n-k); printf("%lld\n",M[Start][Start]); return 0; }
|