BZOJ 1210: [HNOI2004]邮递员

写了两个小时。。。。。。。。

毒瘤三件套:插头DP,哈希判重,高精度

学(抄)到了插头DP的书写艺术

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int base=1e9;
const int maxhash=2601;
int get(int s,int x){return (s>>((x-1)<<1))&3;}
void change(int &s,int p,int b){int w=p-1<<1;s|=3<<w;s^=3<<w;s|=b<<w;}
int find(int s,int p){
	for(int i=p,d=0,step=get(s,p)==1?1:-1;;i+=step){
		d+=get(s,i)==1?1:-1;
		d+=get(s,i)==0?1:0;
		if(!d)return i;
	}
	return -1;
}
int	n,m;
struct bigint{
	int a[5];
	void clear(){memset(a,0,sizeof(a));}
	bigint(){clear();}
	void set(int x){clear();while(x)a[++a[0]]=x%base,x/=base;};
	void print(){printf("%d",a[a[0]]);for(int i=a[0]-1;i>0;i--)printf("%09d",a[i]);}
	bigint operator + (bigint b){
		static bigint c;c.clear();c.a[0]=max(a[0],b.a[0])+1;
		for(int i=1;i<=c.a[0];i++)
		c.a[i]+=a[i]+b.a[i],c.a[i+1]+=c.a[i]/base,c.a[i]%=base;
		while(!c.a[c.a[0]])c.a[0]--;
		return c;
	}
	void operator += (bigint b){*this=*this+b;}
	void operator = (int x){set(x);}
}ans;
struct hashtable{
	int key[maxhash];
	int cnt,hash[maxhash];
	bigint val[maxhash];
	void clear(){
		memset(key,-1,sizeof(key));
		memset(hash,0,sizeof(hash));
		memset(val,0,sizeof(val));
		cnt=0;
	}
	void newnode(int i,int _key){hash[i]=++cnt;key[cnt]=_key;}
	bigint& operator[](const int _hash){
		int k;
		for(int i=_hash%maxhash;;i++){
			if(i==maxhash)i=0;
			if(!hash[i])newnode(i,_hash);
			if(key[hash[i]]==_hash){k=hash[i];break;}
		}
		return val[k];
	}
}f[2];
int now,last;
void dp(int i,int j){
	now^=1;last^=1;
	int tot=f[last].cnt;
	f[now].clear();
	for(int k=1,x,y;k<=tot;k++){
		int s=f[last].key[k];
		bigint val=f[last].val[k];
		x=get(s,j);y=get(s,j+1);
		if(!x&&!y){if(i!=n&&j!=m){change(s,j,1),change(s,j+1,2),f[now][s]+=val;}}
		else if(x==1&&y==1){
			change(s,find(s,j+1),1),change(s,j,0),change(s,j+1,0);
			f[now][s]+=val;
		}else if(x==2&&y==2){
			change(s,find(s,j),2),change(s,j,0),change(s,j+1,0);
			f[now][s]+=val;
		}else if(x==1&&y==2){
			if(i==n&&j==m)ans+=val;
		}else if(x==2&&y==1){
			change(s,j,0),change(s,j+1,0);
			f[now][s]+=val;
		}else if(x){
			if(i!=n)f[now][s]+=val;
			if(j!=m)change(s,j,0),change(s,j+1,x),f[now][s]+=val;
		}else if(y){
			if(j!=m)f[now][s]+=val;
			if(i!=n)change(s,j+1,0),change(s,j,y),f[now][s]+=val;
		}
	}
}
int main(){
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	scanf("%d%d",&n,&m);
	if(n==1||m==1){puts("1");return 0;}
	if(n<m)swap(n,m);
	f[0][0]=1;
	now=0;last=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)dp(i,j);
		if(i!=n)for(int j=1,k=f[now].cnt;j<=k;j++)f[now].key[j]<<=2;
	}
	ans+=ans;
	ans.print();
	return 0;
}


你可能感兴趣的:(BZOJ 1210: [HNOI2004]邮递员)