#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} //ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=10; int n,m,par1,par2,c[20],k; char s[N][N],t[N][N]; int main() { scanf("%d%d",&n,&m); for (int i=0;i<n;i++) { scanf("%s",s[i]); for (int j=0;j<m;j++) { if (s[i][j]=='O') par1^=(i+j)%2,++k; c[(i+j)%2]+=1; } } for (int i=0;i<n;i++) { scanf("%s",t[i]); for (int j=0;j<m;j++) { if (t[i][j]=='O') par2^=(i+j)%2; } } int eg=0; for (int i=0;i<n;i++) for (int j=0;j<m;j++) { if (i+1<n&&(t[i][j]=='O')!=(t[i+1][j]=='O')) ++eg; if (j+1<m&&(t[i][j]=='O')!=(t[i][j+1]=='O')) ++eg; } //fprintf(stderr,"%d\n",eg); if (par1!=par2) { puts("0"); return 0; } int tote=(n-1)*m+n*(m-1); long double ans=0; auto binom=[&](int x,int y) { if (y<0||y>x) return (long double)0; long double ans=1; for (int i=1;i<=y;i++) ans=ans*(x+1-i)/i; return ans; }; for (int p=0;p<=1;p++) { for (int c0=0;c0<=k-1;c0++) if ((k-1-c0+p)%2==par1) { ans+=binom(c[0]-1,c0)*binom(c[1]-1,k-1-c0); } } //printf("%.10Lf\n",ans); cout<<fixed<<setprecision(16)<<eg/(ans*tote)<<endl; }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} //ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=10; int n,m,par1,par2,c[20],k; char s[N][N],t[N][N]; int main() { scanf("%d%d",&n,&m); for (int i=0;i<n;i++) { scanf("%s",s[i]); for (int j=0;j<m;j++) { if (s[i][j]=='O') par1^=(i+j)%2,++k; c[(i+j)%2]+=1; } } for (int i=0;i<n;i++) { scanf("%s",t[i]); for (int j=0;j<m;j++) { if (t[i][j]=='O') par2^=(i+j)%2; } } int eg=0; for (int i=0;i<n;i++) for (int j=0;j<m;j++) { if (i+1<n&&(t[i][j]=='O')!=(t[i+1][j]=='O')) ++eg; if (j+1<m&&(t[i][j]=='O')!=(t[i][j+1]=='O')) ++eg; } //fprintf(stderr,"%d\n",eg); if (par1!=par2) { puts("0"); return 0; } int tote=(n-1)*m+n*(m-1); long double ans=0; auto binom=[&](int x,int y) { if (y<0||y>x) return (long double)0; long double ans=1; for (int i=1;i<=y;i++) ans=ans*(x+1-i)/i; return ans; }; for (int p=0;p<=1;p++) { for (int c0=0;c0<=k-1;c0++) if ((k-1-c0+p)%2==par1) { ans+=binom(c[0]-1,c0)*binom(c[1]-1,k-1-c0); } } //printf("%.10Lf\n",ans); cout<<fixed<<setprecision(16)<<eg/(ans*tote)<<endl; } |