#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ssize(x) int(x.size()) #define pb push_back #define mp make_pair #define all(a) begin(a),end(a) // #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define FOR(i, l, r) for(int i = (l); i < (r); ++i) #define FORE(i, l, r) for(int i = (l); i <= (r); ++i) // # define REP(i, n) FOR(i, 0, (n) - 1) # define REP(i, n) FORE(i, 0, (n) - 1) #define FORR(x,arr) for(auto &x: arr) #define REE(s_) {cout<<s_<<'\n';exit(0);} #define GET(arr) for(auto &i: (arr)) sc(i) #define e1 first #define e2 second #define INF 0x7f7f7f7f #define debug(x...) ; /* #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "),...); }(x), cerr << '\n' */ typedef std::pair<int,int> pi; typedef std::vector<int> vi; typedef std::vector<std::string> vs; typedef int64_t ll; typedef uint64_t ull; using namespace std; using namespace __gnu_pbds; #ifdef ONLINE_JUDGE #define debug(...) {} #endif #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;} inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}} inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}} template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // 1 << 19 if segtree where merges matter. #define N 1000001 int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, m; sc(n, m); string a[n]; FORR(i,a) getstr(i); string b[n]; FORR(i,b) getstr(i); auto get_ile = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ ile += s[i][x] == 'O'; } } return ile; }; auto get_xysum = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ ile += (s[i][x] == 'O') * (i + x); } } return ile & 1; }; int xx[] = {1,-1,0,0}; int yy[] = {0,0,1,-1}; auto get_deg = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ if(s[i][x] == 'O'){ REP(j,4){ int ni = i + xx[j]; int nx = x + yy[j]; /* debug(ni, nx); */ /* if(ni >= 0 && nx >= 0 && ni < n && nx < m){ */ /* debug(ni, nx); */ /* // xd. */ /* if(ni >= 0 && nx >= 0 && ni < n && nx < m && s[i][x] == '.'){ */ /* ++ile; */ /* } */ /* } */ if(ni >= 0 && nx >= 0 && ni < n && nx < m && s[ni][nx] == '.'){ ++ile; } } } } } return ile; }; int ile = get_ile(a); int xysum = get_xysum(a); if(ile != get_ile(b)){ REE(0) } if(xysum != get_xysum(b)){ REE(0) } string c[n]; FORR(i,c) // XDDDD. /* i = string('.', m); */ // thx Zjedzenie zielonej soy przyprawionej zupy soczewicowej zamiast // chad żytniego tradycyjnego. i = string(m, '.'); ll tot = 0; auto rec = [&](auto &self, int i, int x, int rem, string *c) -> void{ /* auto rec = [&](auto &self, int i, int x, int rem){ */ if(x == m){ self(self, i + 1, 0, rem, c); } else if(i == n){ if(rem) return; int crxysum = get_xysum(c); if(crxysum != xysum) return; /* REP(i,n){ */ /* REP(x,m){ */ /* cout << c[i][x]; */ /* } */ /* cout << '\n'; */ /* } */ /* cout << '\n'; */ tot += get_deg(c); } else{ assert(c[i][x] == '.'); self(self, i, x + 1, rem, c); assert(c[i][x] == '.'); if(rem){ c[i][x] = 'O'; self(self, i, x + 1, rem - 1, c); c[i][x] = '.'; } } }; /* debug(get_deg(a)); */ /* exit(0); */ /* rec(rec, 0, 0, ile, c); */ /* pair<ll,ll> dp[n][n][ile + 1][2][1 << n][2]; // -> {cnt, deg sum}. */ pair<ll,ll> dp[n][m][ile + 1][2][1 << m]; // -> {cnt, deg sum}. memset(dp, 0, sizeof dp); /* dp[0][0][ile][0][0][0] = {0,0}; */ /* dp[0][0][ile - 1][0][1][1] = {1,int(m > 1) + int(m > 1)}; */ dp[0][0][ile][0][0] = {1,0}; dp[0][0][ile - 1][0][1] = {1,int(m > 1) + int(n > 1)}; // XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD. // Myślałem że zimplowanie tego dpka będzie giga pojebane i no way że tego // jakoś potężnie nie zbugguje i nie zajmie mi pare h i że najlepiej jak to // zabandonuje i just wezmę te 6 puinktów i tyle. // A tymczasem, after like 10 mins impl, after eleganckim ogarnięciu że can // use 1 mask as combo mask for upper row stuff to the right and cur row // stuff to the left, and after just fixing 1 bug with having 'n' // everywhere, instead of 'm' (like 3 seperate fixes for this one xd) // -> all tests pass. tera sio2 submit let's go. Perhaps last submit in // potyczki. To czy in 2022 or ever, left up to imagination. debug(n, m); REP(i,n){ REP(x,m){ if(!i && !x) continue; REP(crile, ile + 1){ REP(crxysum,2){ REP(prvmask,1<<m){ if(dp[i][x][crile][crxysum][prvmask].e1) debug(i,x,crile,crxysum,prvmask,dp[i][x][crile][crxysum][prvmask]); /* REP(crprv,2){ */ if(1){ // le wrapping technically :) /* if(crprv == 0){ // no take. */ { // no take. int nxmask = prvmask & (((1 << m) - 1) ^ (1 << x)); debug(i, x - 1); dp[i][x][crile][crxysum][nxmask].e1 += dp[i][x-1][crile][crxysum][prvmask].e1; dp[i][x][crile][crxysum][nxmask].e2 += dp[i][x-1][crile][crxysum][prvmask].e2; if(dp[i][x-1][crile][crxysum][prvmask].e1){ debug(prvmask, nxmask); debug(i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2); } debug("post", i, x - 1); } if(crile + 1 <= ile){ // take. int nxmask = prvmask | (1 << x); int nxdeg = ((x > 0) + (i > 0) + (i + 1 < n) + (x + 1 < m)); if(x){ if(prvmask & (1 << (x-1))){ nxdeg -= 2; } } if(prvmask & (1 << x)){ nxdeg -= 2; } dp[i][x][crile][(crxysum + i + x) & 1][nxmask].e2 += dp[i][x-1][crile + 1][crxysum][prvmask].e2 + dp[i][x-1][crile + 1][crxysum][prvmask].e1 * nxdeg; dp[i][x][crile][(crxysum + i + x) & 1][nxmask].e1 += dp[i][x-1][crile + 1][crxysum][prvmask].e1; } } /* } */ } } } } } // Also, mega elegant dp[i][-1] == dp[i-1][0] )). Can't do that in Python can you /* REP(prv,2){ */ /* REP(msk,1<<n){ */ /* tot += dp[n - 1][n - 1][0][xysum][msk][prv].e2; */ /* } */ /* } */ REP(msk,1<<m){ // bruuuh XDD. // why the n vs m mistake wszędzie. /* tot += dp[n - 1][n - 1][0][xysum][msk].e2; */ tot += dp[n - 1][m - 1][0][xysum][msk].e2; } double res = 1.0 * get_deg(b) / tot; /* debug(ile, xysum, tot, get_deg(b)); */ cout << fixed << setprecision(15) << res << '\n'; } /* * [prvmask, nxmask]: 1; 1; * [i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2]: * 0; 4; 0; 0; 1; 1; 1; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * [prvmask, nxmask]: 4; 4; * [i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2]: * 0; 4; 0; 0; 4; 1; 2;*/
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ssize(x) int(x.size()) #define pb push_back #define mp make_pair #define all(a) begin(a),end(a) // #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define FOR(i, l, r) for(int i = (l); i < (r); ++i) #define FORE(i, l, r) for(int i = (l); i <= (r); ++i) // # define REP(i, n) FOR(i, 0, (n) - 1) # define REP(i, n) FORE(i, 0, (n) - 1) #define FORR(x,arr) for(auto &x: arr) #define REE(s_) {cout<<s_<<'\n';exit(0);} #define GET(arr) for(auto &i: (arr)) sc(i) #define e1 first #define e2 second #define INF 0x7f7f7f7f #define debug(x...) ; /* #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "),...); }(x), cerr << '\n' */ typedef std::pair<int,int> pi; typedef std::vector<int> vi; typedef std::vector<std::string> vs; typedef int64_t ll; typedef uint64_t ull; using namespace std; using namespace __gnu_pbds; #ifdef ONLINE_JUDGE #define debug(...) {} #endif #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;} inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}} inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}} template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // 1 << 19 if segtree where merges matter. #define N 1000001 int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, m; sc(n, m); string a[n]; FORR(i,a) getstr(i); string b[n]; FORR(i,b) getstr(i); auto get_ile = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ ile += s[i][x] == 'O'; } } return ile; }; auto get_xysum = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ ile += (s[i][x] == 'O') * (i + x); } } return ile & 1; }; int xx[] = {1,-1,0,0}; int yy[] = {0,0,1,-1}; auto get_deg = [&](string *s){ int ile = 0; REP(i,n){ REP(x,m){ if(s[i][x] == 'O'){ REP(j,4){ int ni = i + xx[j]; int nx = x + yy[j]; /* debug(ni, nx); */ /* if(ni >= 0 && nx >= 0 && ni < n && nx < m){ */ /* debug(ni, nx); */ /* // xd. */ /* if(ni >= 0 && nx >= 0 && ni < n && nx < m && s[i][x] == '.'){ */ /* ++ile; */ /* } */ /* } */ if(ni >= 0 && nx >= 0 && ni < n && nx < m && s[ni][nx] == '.'){ ++ile; } } } } } return ile; }; int ile = get_ile(a); int xysum = get_xysum(a); if(ile != get_ile(b)){ REE(0) } if(xysum != get_xysum(b)){ REE(0) } string c[n]; FORR(i,c) // XDDDD. /* i = string('.', m); */ // thx Zjedzenie zielonej soy przyprawionej zupy soczewicowej zamiast // chad żytniego tradycyjnego. i = string(m, '.'); ll tot = 0; auto rec = [&](auto &self, int i, int x, int rem, string *c) -> void{ /* auto rec = [&](auto &self, int i, int x, int rem){ */ if(x == m){ self(self, i + 1, 0, rem, c); } else if(i == n){ if(rem) return; int crxysum = get_xysum(c); if(crxysum != xysum) return; /* REP(i,n){ */ /* REP(x,m){ */ /* cout << c[i][x]; */ /* } */ /* cout << '\n'; */ /* } */ /* cout << '\n'; */ tot += get_deg(c); } else{ assert(c[i][x] == '.'); self(self, i, x + 1, rem, c); assert(c[i][x] == '.'); if(rem){ c[i][x] = 'O'; self(self, i, x + 1, rem - 1, c); c[i][x] = '.'; } } }; /* debug(get_deg(a)); */ /* exit(0); */ /* rec(rec, 0, 0, ile, c); */ /* pair<ll,ll> dp[n][n][ile + 1][2][1 << n][2]; // -> {cnt, deg sum}. */ pair<ll,ll> dp[n][m][ile + 1][2][1 << m]; // -> {cnt, deg sum}. memset(dp, 0, sizeof dp); /* dp[0][0][ile][0][0][0] = {0,0}; */ /* dp[0][0][ile - 1][0][1][1] = {1,int(m > 1) + int(m > 1)}; */ dp[0][0][ile][0][0] = {1,0}; dp[0][0][ile - 1][0][1] = {1,int(m > 1) + int(n > 1)}; // XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD. // Myślałem że zimplowanie tego dpka będzie giga pojebane i no way że tego // jakoś potężnie nie zbugguje i nie zajmie mi pare h i że najlepiej jak to // zabandonuje i just wezmę te 6 puinktów i tyle. // A tymczasem, after like 10 mins impl, after eleganckim ogarnięciu że can // use 1 mask as combo mask for upper row stuff to the right and cur row // stuff to the left, and after just fixing 1 bug with having 'n' // everywhere, instead of 'm' (like 3 seperate fixes for this one xd) // -> all tests pass. tera sio2 submit let's go. Perhaps last submit in // potyczki. To czy in 2022 or ever, left up to imagination. debug(n, m); REP(i,n){ REP(x,m){ if(!i && !x) continue; REP(crile, ile + 1){ REP(crxysum,2){ REP(prvmask,1<<m){ if(dp[i][x][crile][crxysum][prvmask].e1) debug(i,x,crile,crxysum,prvmask,dp[i][x][crile][crxysum][prvmask]); /* REP(crprv,2){ */ if(1){ // le wrapping technically :) /* if(crprv == 0){ // no take. */ { // no take. int nxmask = prvmask & (((1 << m) - 1) ^ (1 << x)); debug(i, x - 1); dp[i][x][crile][crxysum][nxmask].e1 += dp[i][x-1][crile][crxysum][prvmask].e1; dp[i][x][crile][crxysum][nxmask].e2 += dp[i][x-1][crile][crxysum][prvmask].e2; if(dp[i][x-1][crile][crxysum][prvmask].e1){ debug(prvmask, nxmask); debug(i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2); } debug("post", i, x - 1); } if(crile + 1 <= ile){ // take. int nxmask = prvmask | (1 << x); int nxdeg = ((x > 0) + (i > 0) + (i + 1 < n) + (x + 1 < m)); if(x){ if(prvmask & (1 << (x-1))){ nxdeg -= 2; } } if(prvmask & (1 << x)){ nxdeg -= 2; } dp[i][x][crile][(crxysum + i + x) & 1][nxmask].e2 += dp[i][x-1][crile + 1][crxysum][prvmask].e2 + dp[i][x-1][crile + 1][crxysum][prvmask].e1 * nxdeg; dp[i][x][crile][(crxysum + i + x) & 1][nxmask].e1 += dp[i][x-1][crile + 1][crxysum][prvmask].e1; } } /* } */ } } } } } // Also, mega elegant dp[i][-1] == dp[i-1][0] )). Can't do that in Python can you /* REP(prv,2){ */ /* REP(msk,1<<n){ */ /* tot += dp[n - 1][n - 1][0][xysum][msk][prv].e2; */ /* } */ /* } */ REP(msk,1<<m){ // bruuuh XDD. // why the n vs m mistake wszędzie. /* tot += dp[n - 1][n - 1][0][xysum][msk].e2; */ tot += dp[n - 1][m - 1][0][xysum][msk].e2; } double res = 1.0 * get_deg(b) / tot; /* debug(ile, xysum, tot, get_deg(b)); */ cout << fixed << setprecision(15) << res << '\n'; } /* * [prvmask, nxmask]: 1; 1; * [i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2]: * 0; 4; 0; 0; 1; 1; 1; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * ["post", i, x - 1]: post; 0; 3; * [i, x - 1]: 0; 3; * [prvmask, nxmask]: 4; 4; * [i,x,crile,crxysum,nxmask,dp[i][x][crile][crxysum][nxmask].e1,dp[i][x][crile][crxysum][nxmask].e2]: * 0; 4; 0; 0; 4; 1; 2;*/ |