#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=8; bool tab[N+2][N+2]; long long choose[N*N+10][N*N+10]; long long solve2(int n,int k,int s) { long long ans=0; for(int msk1=0;msk1<(1<<n);msk1++) { for(int msk2=0;msk2<(1<<n);msk2++) { int cnt=__builtin_popcount(msk1)+__builtin_popcount(msk2); if(cnt>k || 2*n-cnt>s-k) continue; ans+=2*__builtin_popcount(msk1&msk2)*choose[s-2*n][k-cnt]; } } return ans; } long long solve1(int n,int k,int s) { long long ans=0; for(int msk1=0;msk1<(1<<n);msk1++) { int cnt=__builtin_popcount(msk1); if(cnt>k || n-cnt>s-k) continue; ans+=cnt*choose[s-n][k-cnt]; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for(int i=0;i<=N*N;i++) { choose[i][0]=1; for(int j=1;j<=i;j++) choose[i][j]=choose[i-1][j-1]+choose[i-1][j]; } int n,m,k=0; cin>>n>>m; int mod2=0,mod22=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; if(c=='O') { mod2=(mod2+i+j)%2; k++; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; if(c=='O') { mod22=(mod22+i+j)%2; tab[i][j]=true; } } } if(mod2!=mod22) { cout<<"0\n"; return 0; } int nom=0; for(int i=1;i<=n;i++) tab[i][0]=tab[i][m+1]=true; for(int i=1;i<=m;i++) tab[0][i]=tab[n+1][i]=true; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(!tab[i][j]) continue; nom+=4; nom-=tab[i][j-1]; nom-=tab[i][j+1]; nom-=tab[i-1][j]; nom-=tab[i+1][j]; } } long long den=4*k*choose[n*m][k]; //cerr<<den<<"\n"; den-=solve2(n,k,n*m)*(m-1); //cerr<<den<<"\n"; den-=solve2(m,k,n*m)*(n-1); //cerr<<den<<"\n"; den-=solve1(n,k,n*m)*2; //cerr<<den<<"\n"; den-=solve1(m,k,n*m)*2; //cerr<<den<<"\n"; den/=2; //cerr<<nom<<"/"<<den<<"\n"; cout<<fixed<<setprecision(15)<<(long double)nom/den<<"\n"; 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 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=8; bool tab[N+2][N+2]; long long choose[N*N+10][N*N+10]; long long solve2(int n,int k,int s) { long long ans=0; for(int msk1=0;msk1<(1<<n);msk1++) { for(int msk2=0;msk2<(1<<n);msk2++) { int cnt=__builtin_popcount(msk1)+__builtin_popcount(msk2); if(cnt>k || 2*n-cnt>s-k) continue; ans+=2*__builtin_popcount(msk1&msk2)*choose[s-2*n][k-cnt]; } } return ans; } long long solve1(int n,int k,int s) { long long ans=0; for(int msk1=0;msk1<(1<<n);msk1++) { int cnt=__builtin_popcount(msk1); if(cnt>k || n-cnt>s-k) continue; ans+=cnt*choose[s-n][k-cnt]; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for(int i=0;i<=N*N;i++) { choose[i][0]=1; for(int j=1;j<=i;j++) choose[i][j]=choose[i-1][j-1]+choose[i-1][j]; } int n,m,k=0; cin>>n>>m; int mod2=0,mod22=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; if(c=='O') { mod2=(mod2+i+j)%2; k++; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; if(c=='O') { mod22=(mod22+i+j)%2; tab[i][j]=true; } } } if(mod2!=mod22) { cout<<"0\n"; return 0; } int nom=0; for(int i=1;i<=n;i++) tab[i][0]=tab[i][m+1]=true; for(int i=1;i<=m;i++) tab[0][i]=tab[n+1][i]=true; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(!tab[i][j]) continue; nom+=4; nom-=tab[i][j-1]; nom-=tab[i][j+1]; nom-=tab[i-1][j]; nom-=tab[i+1][j]; } } long long den=4*k*choose[n*m][k]; //cerr<<den<<"\n"; den-=solve2(n,k,n*m)*(m-1); //cerr<<den<<"\n"; den-=solve2(m,k,n*m)*(n-1); //cerr<<den<<"\n"; den-=solve1(n,k,n*m)*2; //cerr<<den<<"\n"; den-=solve1(m,k,n*m)*2; //cerr<<den<<"\n"; den/=2; //cerr<<nom<<"/"<<den<<"\n"; cout<<fixed<<setprecision(15)<<(long double)nom/den<<"\n"; return 0; } |