您的位置:首页 > Web前端

HDU 5779 Tower Defence

2016-07-30 22:21 225 查看
求有多少个n个点的、边权为1的无向图满足从1号点到其他任意一个点的最短路都不等于k,不连通距离为inf。

n,k≤60.

从前有个蒟蒻,连个O(n^4)的dp都写不好,循环分不清南北==

#include<set>
#include<ctime>
#include<queue>
#include<cstdio>
#include<bitset>
#include<cctype>
#include<bitset>
#include<cstdlib>
#include<cassert>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
int f=1;char c;x=0;
while(c=getchar(),c<48)if(c=='-')f=-1;
do x=x*10+(c^48);
while(c=getchar(),c>47);
x*=f;
}
template<class T>void nt(T x){
if(!x)return;
nt(x/10);
putchar(x%10+'0');
}
template<class T>void pt(T x){
if(x<0)putchar('-'),x=-x;
if(!x)putchar('0');
else nt(x);
}
template<class T>void ptn(T x){
pt(x);putchar('\n');
}
template<class T>void pts(T x){
pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

const int maxn=65;
ll dp[maxn][maxn][maxn];//dp[i][j][k] 共j个点 第i层有k个点
ll dp2[maxn][maxn];
ll dp3[maxn][maxn];
const int mod=1e9+7;
ll cc[maxn][maxn];
ll tt[maxn*maxn];
ll qpow(ll a,ll b){
ll c=1;
for(;b;b>>=1,a=(a*a)%mod)
if(b&1)c=(a*c)%mod;
return c;
}
int main(){
rep(i,0,maxn){
rep(j,1,i)cc[i][j]=(cc[i-1][j-1]+cc[i-1][j])%mod;
cc[i][0]=cc[i][i]=1;
}
tt[0]=1;
rep(i,1,maxn*maxn)
tt[i]=(tt[i-1]<<1)%mod;
//预处理2的幂

dp[1][1][1]=1;//1层的方案仅一种
for(int i=1;i+1<maxn;i++){//正推
for(int j=i;j<maxn;j++){//枚举前i层有几个
for(int k=1;k<=j-i+1;k++){//枚举....=,=写反了呢
if(!dp[i][j][k])continue;
for(int s=1;j+s<maxn;s++){
dp[i+1][j+s][s]+=dp[i][j][k]*cc[j+s-1][s]%mod*qpow(tt[k]-1,s)%mod*tt[s*(s-1)/2]%mod;
dp[i+1][j+s][s]%=mod;
}
}
}
}
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
for(int k=0;k<maxn;k++){
dp2[i][j]+=dp[i][j][k];
dp2[i][j]%=mod;
}

for(int i=1;i<maxn;i++){//层数上限
for(int j=1;j<maxn;j++){//个数上限
for(int k=1;k<=i;k++){//
for(int s=1;s<=j;s++){
dp3[i][j]+=dp2[k][s]*cc[j-1][j-s]%mod*tt[(j-s)*(j-s-1)/2]%mod;
dp3[i][j]%=mod;
}
}
}
}

int cas,a,b;sc(cas);
while(cas--){
sc(a);sc(b);
ptn(dp3[b][a]);
}
return 0;
}


蒟蒻觉得自己很可怜,于是又打了个表。

puts("A[60][60]={");
for(int a=0;a<60;a++){
printf("{");
for(int b=0;b<60;b++){
cout<<dp3[b+1][a+1];
if(b!=59)printf(",");
else printf("}");
}
puts(a!=59?",":"");
}
puts("};");


但因为这是蒟蒻打的第一个二维的表,所以写了博客。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: