BZOJ3939 : [Usaco2015 Feb]Cow Hopscotch

设f[i][j]表示到(i,j)的方案数,则有

$f[i][j]=\sum f[x][y](x<i,y<j,a[x][y]!=a[i][j])=\sum f[x][y](x<i,y<j)-\sum f[x][y](x<i,y<j,a[x][y]==a[i][j])$

然后运用CDQ分治即可$O(nm\log n)$解决。

 

#include<cstdio>
const int N=752,P=1000000007;
int n,m,k,i,j,a[N][N],f[N][N],T,all,s[N*N],v[N*N];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
void solve(int l,int r){
  if(l==r)return;
  int mid=(l+r)>>1;
  solve(l,mid);
  for(T++,all=0,j=1;j<=m;j++){
    for(i=r;i>mid;i--){
      if(v[a[i][j]]<T)v[a[i][j]]=T,s[a[i][j]]=0;
      f[i][j]=((f[i][j]+all-s[a[i][j]])%P+P)%P;
    }
    for(i=l;i<=mid;i++){
      if(v[a[i][j]]<T)v[a[i][j]]=T,s[a[i][j]]=0;
      (s[a[i][j]]+=f[i][j])%=P,(all+=f[i][j])%=P;
    }
  }
  solve(mid+1,r);
}
int main(){
  read(n),read(m),read(k);
  for(i=f[1][1]=1;i<=n;i++)for(j=1;j<=m;j++)read(a[i][j]);
  solve(1,n);
  return printf("%d",f[n][m]),0;
}

  

posted @ 2015-04-17 13:58  Claris  阅读(759)  评论(0编辑  收藏  举报