您的位置:首页 > 其它

bzoj 2004: [Hnoi2010]Bus 公交线路

2017-08-22 15:42 393 查看

题意:

按下述规则设计线路:

1.设共K辆公交车,则1到K号站作为始发站,N-K+1到N号台作为终点站。

2.每个车站必须被一辆且仅一辆公交车经过(始发站和

终点站也算被经过)。

3.公交车只能从编号较小的站台驶往编号较大的站台。

4.一辆公交车经过的相邻两个

求出答案对30031取模的结果。

所谓的经过是指停车。

题解:

状压dp+矩乘。

首先多辆车的顺序是无所谓的,所以我们可以强制让所有车在p的范围内。

状压这时每个站是否停车,因为要保证忽略顺序和每个站有且仅有停过一次车,所以要保证最左边要有一辆车,每次转移也这能动最左的车。

最多有k个1,所以状态总数有C(4,9),矩乘可以接受。

code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<iostream>
using namespace std;
const int mod=30031;
map<int,int> s,t;
int n,k,p,tot=0;
struct node{
int a[130][130];
node(){memset(a,0,sizeof(a));}
};
void dfs(int x,int num,int now)
{
if(num>k) return;
if(x>p)
{
if(num==k) s[now]=++tot,t[tot]=now;
return;
}
dfs(x+1,num+1,(now<<1)+1);
dfs(x+1,num,now<<1);
}
bool check(int from,int to)
{
from=(from-(1<<(p-1)))<<1;
int tmp=from^to;
if(tmp==(tmp&(-tmp))) return true;
return false;
}
node cheng(node a,node b)
{
node ans;
for(int i=1;i<=tot;i++)
for(int j=1;j<=tot;j++)
for(int k=1;k<=tot;k++)
(ans.a[i][j]+=a.a[i][k]*b.a[k][j])%=mod;
return ans;
}
int main()
{
scanf("%d %d %d",&n,&k,&p);
dfs(2,1,1);
node a;
for(int i=1;i<=tot;i++)
for(int j=1;j<=tot;j++)
if(check(t[i],t[j])) a.a[i][j]=1;
node ans;int b=n-k;
for(int i=1;i<=tot;i++) ans.a[i][i]=1;
while(b)
{
if(b&1) ans=cheng(ans,a);
a=cheng(a,a);b>>=1;
}
int ANS=0;
printf("%d",ans.a[1][1]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: