#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vi prep; int solve(int n, int m, vector<vector<int> > V, bool b){ while(true){ //for(int i=0; i<n; i++){for(int j=0; j<m; j++){cout<<V[i][j]<<" \n"[j==m-1];}}cout<<"\n"; int opt=25; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(!V[i][j])continue; int deg=0; if(i && V[i-1][j])deg++; if(i<n-1 && V[i+1][j])deg++; if(j && V[i][j-1])deg++; if(j<m-1 && V[i][j+1])deg++; if(deg){ assert(V[i][j]%deg==0); if(V[i][j]/deg<opt){ opt=V[i][j]/deg; } } } } //cout<<opt<<"\n"; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(V[i][j]){ if(i && V[i-1][j])V[i][j]-=opt, V[i-1][j]-=opt; if(j && V[i][j-1])V[i][j]-=opt, V[i][j-1]-=opt; } } } if(opt==25)break; } if(b==1){ return V[0][m/2]; } int ans=0; for(int i=0; i<n; i++){ for(int j=0; j<m ;j++){ ans+=V[i][j]; } } return ans; } void gen(int n, int m){ vector<vector<int> > V(n, vi(m)); for(int i=0; i<(1<<(n*m)); i++){ for(int j=0; j<(n*m);j ++){ V[j/m][j%m]=(!!(i&(1<<j)))*24; } prep.pb(solve(n, m, V, n!=4)); } } int main(){ //BOOST; int n, m; cin>>n>>m; vector<str> co(n); vector<vector<int> > V(n, vi(m)); //deb(solve(2, 1, {{24}, {24}}, 0)); bool b=0; for(int i=0; i<n; i++){ cin>>co[i]; for(int j=0; j<m; j++){ V[i][j]=24*(co[i][j]=='O'); if(co[i][j]!='.' && !((i*j)%5)){ b=0; } } } if(n<3){ int d=1+n; gen(n, 2*d+1); ll ans=0;//mnoznik 2^20*24 for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ vector<int> zmi; int ile=0, ile2=0; for(int k=-d; k<=d; k++){ if(j+k>=0 && j+k<m){ //deb(k, d, ile, ile2); if(co[i][j+k]=='O')ile+=(1<<(k+d)); if(co[i][j+k]=='?')ile2+=(1<<(k+d)); if(n==2){ if(co[i^1][j+k]=='O')ile+=(1<<(k+3*d+1)); if(co[i^1][j+k]=='?')ile2+=(1<<(k+3*d+1)); } } } //deb(i, j, ile, d, ile2); int po=20-__builtin_popcount(ile2); for(int mask=ile2; ; mask=(mask-1)&ile2){ //deb(mask+ile, prep[mask+ile]); ans+=prep[mask+ile]<<po; if(mask==0)break; } } } ll x=24<<20; ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; return 0; } else{ if(b){ //deb("a"); gen(4, 4); ll ans=0; for(int i=1; i<n; i+=5){ for(int j=1;j<m; j+=5){ int ile=0, ile2=0; for(int x=0; x+i<n && x<4; x++){ for(int y=0; y+j<m && y<4; y++){ if(co[i+x][j+y]=='O')ile+=(1<<(4*y+x)); if(co[i+x][j+y]=='?')ile2+=(1<<(4*y+x)); } } int po=20-__builtin_popcount(ile2); for(int mask=ile2; ; mask=(mask-1)&ile2){ //deb(mask+ile, prep[mask+ile]); ans+=prep[mask+ile]<<po; if(mask==0)break; } } } ll x=24<<20; ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; return 0; } ll ans=0; vii zmi; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(co[i][j]=='?')zmi.eb(i, j); } } for(int mask=0; mask<(1<<zmi.size()); mask++){ for(int i=0; i<zmi.size(); i++){ if(mask&(1<<i))V[zmi[i].st][zmi[i].nd]=24; else V[zmi[i].st][zmi[i].nd]=0; } //deb(V, solve(n, m, V, 0)); ans+=solve(n, m, V, 0); } ll x=24<<zmi.size(); ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; 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 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vi prep; int solve(int n, int m, vector<vector<int> > V, bool b){ while(true){ //for(int i=0; i<n; i++){for(int j=0; j<m; j++){cout<<V[i][j]<<" \n"[j==m-1];}}cout<<"\n"; int opt=25; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(!V[i][j])continue; int deg=0; if(i && V[i-1][j])deg++; if(i<n-1 && V[i+1][j])deg++; if(j && V[i][j-1])deg++; if(j<m-1 && V[i][j+1])deg++; if(deg){ assert(V[i][j]%deg==0); if(V[i][j]/deg<opt){ opt=V[i][j]/deg; } } } } //cout<<opt<<"\n"; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(V[i][j]){ if(i && V[i-1][j])V[i][j]-=opt, V[i-1][j]-=opt; if(j && V[i][j-1])V[i][j]-=opt, V[i][j-1]-=opt; } } } if(opt==25)break; } if(b==1){ return V[0][m/2]; } int ans=0; for(int i=0; i<n; i++){ for(int j=0; j<m ;j++){ ans+=V[i][j]; } } return ans; } void gen(int n, int m){ vector<vector<int> > V(n, vi(m)); for(int i=0; i<(1<<(n*m)); i++){ for(int j=0; j<(n*m);j ++){ V[j/m][j%m]=(!!(i&(1<<j)))*24; } prep.pb(solve(n, m, V, n!=4)); } } int main(){ //BOOST; int n, m; cin>>n>>m; vector<str> co(n); vector<vector<int> > V(n, vi(m)); //deb(solve(2, 1, {{24}, {24}}, 0)); bool b=0; for(int i=0; i<n; i++){ cin>>co[i]; for(int j=0; j<m; j++){ V[i][j]=24*(co[i][j]=='O'); if(co[i][j]!='.' && !((i*j)%5)){ b=0; } } } if(n<3){ int d=1+n; gen(n, 2*d+1); ll ans=0;//mnoznik 2^20*24 for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ vector<int> zmi; int ile=0, ile2=0; for(int k=-d; k<=d; k++){ if(j+k>=0 && j+k<m){ //deb(k, d, ile, ile2); if(co[i][j+k]=='O')ile+=(1<<(k+d)); if(co[i][j+k]=='?')ile2+=(1<<(k+d)); if(n==2){ if(co[i^1][j+k]=='O')ile+=(1<<(k+3*d+1)); if(co[i^1][j+k]=='?')ile2+=(1<<(k+3*d+1)); } } } //deb(i, j, ile, d, ile2); int po=20-__builtin_popcount(ile2); for(int mask=ile2; ; mask=(mask-1)&ile2){ //deb(mask+ile, prep[mask+ile]); ans+=prep[mask+ile]<<po; if(mask==0)break; } } } ll x=24<<20; ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; return 0; } else{ if(b){ //deb("a"); gen(4, 4); ll ans=0; for(int i=1; i<n; i+=5){ for(int j=1;j<m; j+=5){ int ile=0, ile2=0; for(int x=0; x+i<n && x<4; x++){ for(int y=0; y+j<m && y<4; y++){ if(co[i+x][j+y]=='O')ile+=(1<<(4*y+x)); if(co[i+x][j+y]=='?')ile2+=(1<<(4*y+x)); } } int po=20-__builtin_popcount(ile2); for(int mask=ile2; ; mask=(mask-1)&ile2){ //deb(mask+ile, prep[mask+ile]); ans+=prep[mask+ile]<<po; if(mask==0)break; } } } ll x=24<<20; ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; return 0; } ll ans=0; vii zmi; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(co[i][j]=='?')zmi.eb(i, j); } } for(int mask=0; mask<(1<<zmi.size()); mask++){ for(int i=0; i<zmi.size(); i++){ if(mask&(1<<i))V[zmi[i].st][zmi[i].nd]=24; else V[zmi[i].st][zmi[i].nd]=0; } //deb(V, solve(n, m, V, 0)); ans+=solve(n, m, V, 0); } ll x=24<<zmi.size(); ll nwd=__gcd(ans, x); cout<<ans/nwd<<"/"<<x/nwd; return 0; } } |