#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int gox[5]={1,0,-1,0,0},goy[5]={0,1,0,-1,0}; const int mo=1e9+7; int sign; int check(int k1,int k2,int k3,int k4){ for (int i=0;i<4;i++) if (k3==k1+gox[i]&&k4==k2+goy[i]) return 1; return 0; } struct pattern{ int A[8],B[8]; int n,m,len,pd[16],del[8],a[100],b[100],tot; void print(){ cout<<"asd"<<endl; int bo[10][10]; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) bo[i][j]=0; for (int i=1;i<=len;i++) bo[A[i]][B[i]]=1; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++) printf("%d",bo[i][j]); putchar('\n'); } //for (int i=1;i<=len;i++) cout<<A[i]<<" "<<B[i]<<endl; } void init(){ for (int i=1;i<=len;i++){ del[i]=0; for (int j=1;j<=len;j++) if (i!=j) del[i]+=check(A[i],B[i],A[j],B[j]); } tot=0; for (int i=1;i<=len;i++) for (int j=0;j<5;j++){ int aa=A[i]+gox[j],bb=B[i]+goy[j],flag=0; for (int k=1;k<=tot;k++) if (a[k]==aa&&b[k]==bb){ flag=1; break; } if (flag==0){ tot++; a[tot]=aa; b[tot]=bb; } } if (len!=4){ for (int i=0;i<(1<<len);i++) pd[i]=1; pd[0]=0; return; } memset(pd,0x00,sizeof pd); for (int i=1;i<=4;i++) for (int j=i+1;j<=4;j++){ int flag=0; int where1=1; while (where1==i||where1==j) where1++; int where2=len; while (where2==i||where2==j) where2--; if ((check(A[i],B[i],A[where1],B[where1])&&check(A[j],B[j],A[where2],B[where2]))){ pd[(1<<i-1)+(1<<j-1)]++; } swap(where1,where2); if ((check(A[i],B[i],A[where1],B[where1])&&check(A[j],B[j],A[where2],B[where2]))){ pd[(1<<i-1)+(1<<j-1)]++; } } for (int i=1;i<(1<<len);i++) pd[i]=1-pd[i]; // print(); // for (int i=0;i<16;i++) if (pd[i]) cout<<i<<" "; cout<<endl; } }; vector<pattern>x[5]; int K,n,A[10][2]; char ch[3100]; char pd[3100][3100],imp[3100][3100]; int qua(int k1,int k2){ return k1>0&&k1<=n&&k2>0&&k2<=n; } void dfs(int k1,int k2){ pd[k1][k2]=2; for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (qua(a,b)&&pd[a][b]==1) dfs(a,b); } } void check(){ int flag=0; for (int i=1;i<=n;i++) flag|=pd[1][i]; if (flag==0) return; flag=0; for (int i=1;i<=n;i++) flag|=pd[i][1]; if (flag==0) return; flag=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]==1){ dfs(i,j); flag++; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]) pd[i][j]=1; if (flag==1){ pattern k1; k1.len=K; for (int i=1;i<=K;i++) k1.A[i]=A[i][0],k1.B[i]=A[i][1]; k1.n=0; k1.m=0; for (int i=1;i<=K;i++) k1.n=max(k1.n,A[i][0]),k1.m=max(k1.m,A[i][1]); k1.init(); x[K].push_back(k1); } } void dfs(int k1,int k2,int k3){ if (k3>n){ k3=1; k2++; } if (k2>n){ if (k1==K+1) check(); return; } dfs(k1,k2,k3+1); if (k1>K) return; pd[k2][k3]=1; A[k1][0]=k2; A[k1][1]=k3; dfs(k1+1,k2,k3+1); pd[k2][k3]=0; } void getpattern(int _K){ K=_K; n=_K; dfs(1,1,1); } int impnum,ans; int quick(int k1,int k2){ int k3=1; while (k2){ if (k2&1) k3=1ll*k3*k1%mo; k2>>=1; k1=1ll*k1*k1%mo; } return k3; } int allimp(){ int ans=1; for (int i=1;i<=K;i++) ans=1ll*ans*(impnum-i+1)%mo; for (int i=1;i<=K;i++) ans=1ll*ans*quick(i,mo-2)%mo; return ans; } int matchpattern(pattern& K){ int ans=0; //K.print(); for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=(imp[a][b]<<k-1); } // cout<<ti<<" "<<K.pd[ti]<<endl; if (bo==0&&flag){ ans+=K.pd[ti]; // cout<<"find "<<i<<" "<<j<<endl; } } return ans; } char w1[3100][3100],w2[3100][3100]; int del(int k1,int k2){ int ans=0; //cout<<"del "<<k1<<" "<<k2<<endl; if (imp[k1][k2]==0){ ans--; for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (imp[a][b]) ans++; } } else if (imp[k1][k2]){ for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (imp[a][b]^pd[a][b]) ans++; } } return ans; } void getw1(){ for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) w1[i][j]=del(i,j); } int getwround(int a,int b){ int ans=0; for (int kk=0;kk<5;kk++){ int aa=a+gox[kk],bb=b+goy[kk]; if (imp[aa][bb]) ans++; } return ans; } void getw2(){ for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) w2[i][j]=getwround(i,j); } int matchwithonepoint(pattern& K){ // K.print(); long long ans=0; for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=imp[a][b]; } //cout<<i<<" "<<j<<" "<<flag<<" "<<bo<<endl; if (bo||flag==0||ti==0) continue; //cout<<"asd "<<i<<" "<<j<<endl; int nowpoint=0; for (int k=1;k<=K.tot;k++){ int a=K.a[k]+i,b=K.b[k]+j; nowpoint+=imp[a][b]; } // cout<<"fa "<<nowpoint<<" "<<impnum<<endl; ans=(ans+impnum-nowpoint); } return ans%mo; } int matchwithtwopoint(pattern& K){ // K.print(); int ans=0; for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=imp[a][b]; } //cout<<i<<" "<<j<<" "<<flag<<" "<<bo<<endl; if (bo||flag==0||ti==0) continue; // cout<<"asd "<<i<<" "<<j<<endl; int nowpoint=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; nowpoint+=w2[a][b]; if (imp[a][b]) nowpoint-=K.del[k]; } int rem=impnum-nowpoint; ans=(ans+1ll*rem*(rem-1)/2)%mo; } return ans; } int matchtwonotimp(){ int totnum=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (imp[i][j]==0&&pd[i][j]) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (imp[a][b]) totnum++; } long long ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (imp[i][j]==0&&pd[i][j]) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (imp[a][b]){ sign++; int num=w1[i][j]+w1[a][b]; ans+=totnum-num; } } return ans/2%mo; } int main(){ for (int i=1;i<=4;i++) getpattern(i); scanf("%d%d",&n,&K); //n=3000; for (int i=1;i<=n;i++){ scanf("%s",ch+1); for (int j=1;j<=n;j++){ pd[i][j]=(rand()%1000==0); pd[i][j]=(ch[j]=='.'); } } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]==0) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (pd[a][b]) imp[a][b]=1; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) impnum+=imp[i][j]; /* for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<(int)imp[i][j]; cout<<endl;} cout<<endl; for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<(int)pd[i][j]; cout<<endl;}*/ ans=allimp(); //cout<<ans<<" "<<impnum<<endl; getw1(); getw2(); if (K==1){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K].size();i++) ans=(ans+matchpattern(x[K][i]))%mo; //cout<<ans<<endl; if (K==2){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K-1].size();i++) ans=(ans+matchwithonepoint(x[K-1][i]))%mo;// cout<<ans<<endl; if (K==3){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K-2].size();i++) ans=(ans+matchwithtwopoint(x[K-2][i]))%mo;// cout<<ans<<endl; ans=(ans+matchtwonotimp())%mo; printf("%d\n",ans); return 0; }
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int gox[5]={1,0,-1,0,0},goy[5]={0,1,0,-1,0}; const int mo=1e9+7; int sign; int check(int k1,int k2,int k3,int k4){ for (int i=0;i<4;i++) if (k3==k1+gox[i]&&k4==k2+goy[i]) return 1; return 0; } struct pattern{ int A[8],B[8]; int n,m,len,pd[16],del[8],a[100],b[100],tot; void print(){ cout<<"asd"<<endl; int bo[10][10]; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) bo[i][j]=0; for (int i=1;i<=len;i++) bo[A[i]][B[i]]=1; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++) printf("%d",bo[i][j]); putchar('\n'); } //for (int i=1;i<=len;i++) cout<<A[i]<<" "<<B[i]<<endl; } void init(){ for (int i=1;i<=len;i++){ del[i]=0; for (int j=1;j<=len;j++) if (i!=j) del[i]+=check(A[i],B[i],A[j],B[j]); } tot=0; for (int i=1;i<=len;i++) for (int j=0;j<5;j++){ int aa=A[i]+gox[j],bb=B[i]+goy[j],flag=0; for (int k=1;k<=tot;k++) if (a[k]==aa&&b[k]==bb){ flag=1; break; } if (flag==0){ tot++; a[tot]=aa; b[tot]=bb; } } if (len!=4){ for (int i=0;i<(1<<len);i++) pd[i]=1; pd[0]=0; return; } memset(pd,0x00,sizeof pd); for (int i=1;i<=4;i++) for (int j=i+1;j<=4;j++){ int flag=0; int where1=1; while (where1==i||where1==j) where1++; int where2=len; while (where2==i||where2==j) where2--; if ((check(A[i],B[i],A[where1],B[where1])&&check(A[j],B[j],A[where2],B[where2]))){ pd[(1<<i-1)+(1<<j-1)]++; } swap(where1,where2); if ((check(A[i],B[i],A[where1],B[where1])&&check(A[j],B[j],A[where2],B[where2]))){ pd[(1<<i-1)+(1<<j-1)]++; } } for (int i=1;i<(1<<len);i++) pd[i]=1-pd[i]; // print(); // for (int i=0;i<16;i++) if (pd[i]) cout<<i<<" "; cout<<endl; } }; vector<pattern>x[5]; int K,n,A[10][2]; char ch[3100]; char pd[3100][3100],imp[3100][3100]; int qua(int k1,int k2){ return k1>0&&k1<=n&&k2>0&&k2<=n; } void dfs(int k1,int k2){ pd[k1][k2]=2; for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (qua(a,b)&&pd[a][b]==1) dfs(a,b); } } void check(){ int flag=0; for (int i=1;i<=n;i++) flag|=pd[1][i]; if (flag==0) return; flag=0; for (int i=1;i<=n;i++) flag|=pd[i][1]; if (flag==0) return; flag=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]==1){ dfs(i,j); flag++; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]) pd[i][j]=1; if (flag==1){ pattern k1; k1.len=K; for (int i=1;i<=K;i++) k1.A[i]=A[i][0],k1.B[i]=A[i][1]; k1.n=0; k1.m=0; for (int i=1;i<=K;i++) k1.n=max(k1.n,A[i][0]),k1.m=max(k1.m,A[i][1]); k1.init(); x[K].push_back(k1); } } void dfs(int k1,int k2,int k3){ if (k3>n){ k3=1; k2++; } if (k2>n){ if (k1==K+1) check(); return; } dfs(k1,k2,k3+1); if (k1>K) return; pd[k2][k3]=1; A[k1][0]=k2; A[k1][1]=k3; dfs(k1+1,k2,k3+1); pd[k2][k3]=0; } void getpattern(int _K){ K=_K; n=_K; dfs(1,1,1); } int impnum,ans; int quick(int k1,int k2){ int k3=1; while (k2){ if (k2&1) k3=1ll*k3*k1%mo; k2>>=1; k1=1ll*k1*k1%mo; } return k3; } int allimp(){ int ans=1; for (int i=1;i<=K;i++) ans=1ll*ans*(impnum-i+1)%mo; for (int i=1;i<=K;i++) ans=1ll*ans*quick(i,mo-2)%mo; return ans; } int matchpattern(pattern& K){ int ans=0; //K.print(); for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=(imp[a][b]<<k-1); } // cout<<ti<<" "<<K.pd[ti]<<endl; if (bo==0&&flag){ ans+=K.pd[ti]; // cout<<"find "<<i<<" "<<j<<endl; } } return ans; } char w1[3100][3100],w2[3100][3100]; int del(int k1,int k2){ int ans=0; //cout<<"del "<<k1<<" "<<k2<<endl; if (imp[k1][k2]==0){ ans--; for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (imp[a][b]) ans++; } } else if (imp[k1][k2]){ for (int i=0;i<4;i++){ int a=k1+gox[i],b=k2+goy[i]; if (imp[a][b]^pd[a][b]) ans++; } } return ans; } void getw1(){ for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) w1[i][j]=del(i,j); } int getwround(int a,int b){ int ans=0; for (int kk=0;kk<5;kk++){ int aa=a+gox[kk],bb=b+goy[kk]; if (imp[aa][bb]) ans++; } return ans; } void getw2(){ for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) w2[i][j]=getwround(i,j); } int matchwithonepoint(pattern& K){ // K.print(); long long ans=0; for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=imp[a][b]; } //cout<<i<<" "<<j<<" "<<flag<<" "<<bo<<endl; if (bo||flag==0||ti==0) continue; //cout<<"asd "<<i<<" "<<j<<endl; int nowpoint=0; for (int k=1;k<=K.tot;k++){ int a=K.a[k]+i,b=K.b[k]+j; nowpoint+=imp[a][b]; } // cout<<"fa "<<nowpoint<<" "<<impnum<<endl; ans=(ans+impnum-nowpoint); } return ans%mo; } int matchwithtwopoint(pattern& K){ // K.print(); int ans=0; for (int i=0;i<=n-K.n;i++) for (int j=0;j<=n-K.m;j++){ int bo=1,flag=1,ti=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; flag&=pd[a][b]; bo&=imp[a][b]; ti|=imp[a][b]; } //cout<<i<<" "<<j<<" "<<flag<<" "<<bo<<endl; if (bo||flag==0||ti==0) continue; // cout<<"asd "<<i<<" "<<j<<endl; int nowpoint=0; for (int k=1;k<=K.len;k++){ int a=K.A[k]+i,b=K.B[k]+j; nowpoint+=w2[a][b]; if (imp[a][b]) nowpoint-=K.del[k]; } int rem=impnum-nowpoint; ans=(ans+1ll*rem*(rem-1)/2)%mo; } return ans; } int matchtwonotimp(){ int totnum=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (imp[i][j]==0&&pd[i][j]) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (imp[a][b]) totnum++; } long long ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (imp[i][j]==0&&pd[i][j]) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (imp[a][b]){ sign++; int num=w1[i][j]+w1[a][b]; ans+=totnum-num; } } return ans/2%mo; } int main(){ for (int i=1;i<=4;i++) getpattern(i); scanf("%d%d",&n,&K); //n=3000; for (int i=1;i<=n;i++){ scanf("%s",ch+1); for (int j=1;j<=n;j++){ pd[i][j]=(rand()%1000==0); pd[i][j]=(ch[j]=='.'); } } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (pd[i][j]==0) for (int k=0;k<4;k++){ int a=i+gox[k],b=j+goy[k]; if (pd[a][b]) imp[a][b]=1; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) impnum+=imp[i][j]; /* for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<(int)imp[i][j]; cout<<endl;} cout<<endl; for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<(int)pd[i][j]; cout<<endl;}*/ ans=allimp(); //cout<<ans<<" "<<impnum<<endl; getw1(); getw2(); if (K==1){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K].size();i++) ans=(ans+matchpattern(x[K][i]))%mo; //cout<<ans<<endl; if (K==2){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K-1].size();i++) ans=(ans+matchwithonepoint(x[K-1][i]))%mo;// cout<<ans<<endl; if (K==3){ printf("%d\n",ans); return 0; } for (int i=0;i<x[K-2].size();i++) ans=(ans+matchwithtwopoint(x[K-2][i]))%mo;// cout<<ans<<endl; ans=(ans+matchtwonotimp())%mo; printf("%d\n",ans); return 0; } |