#include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define NM 8 #define CNT 8 #define SZ (1<<NM) int tmsk[NM]; char s[NM][NM+8], t[NM][NM+8]; int row_deg[SZ]; pull dp[NM][CNT+1][SZ][2]; /// row, cnt, msk, par -> deg(cnt, sum) int row_par(int row, int msk) { return popcnt((0xaa>>(row%2))&msk)%2; } void update(pull &dst, pull src, int deg) { dst.fir += src.fir; dst.sec += src.sec+src.fir*deg; } int main() { canhazfast(); int n, m, sz, gmsk; int spar = 0, tpar = 0, stcnt = 0; ull tdeg, totcnt = 0, degsum = 0; cin >> n >> m; sz = 1<<m; gmsk = sz-1; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < m; ++j) { if (s[i][j] == 'O') { ++stcnt; spar ^= (i^j)&1; } } } for (int i = 0; i < n; ++i) { cin >> t[i]; for (int j = 0; j < m; ++j) { if (t[i][j] == 'O') { tmsk[i] |= 1<<j; tpar ^= (i^j)&1; } } } retif(spar != tpar) cout << "0\n", 0; for (int msk = 0; msk < sz; ++msk) row_deg[msk] = popcnt((msk^(msk>>1))&(gmsk>>1)); tdeg = row_deg[tmsk[0]]; for (int i = 1; i < n; ++i) { tdeg += row_deg[tmsk[i]]; tdeg += popcnt(tmsk[i-1]^tmsk[i]); } //cerr << "init:\nmsk\tpcnt\tpar\tdeg\n"; for (int msk = 0; msk < sz; ++msk) { int cnt = popcnt(msk); int par = row_par(0, msk); dp[0][cnt][msk][par] = {1, row_deg[msk]}; //cerr << bitset<5>(msk) << '\t' << cnt << '\t' << par << '\t' << row_deg[msk] << '\n'; } for (int i = 1; i < n; ++i) { for (int cur_cnt = 0; cur_cnt <= stcnt; ++cur_cnt) { for (int cur_msk = 0; cur_msk < sz; ++cur_msk) { int pre_cnt = cur_cnt-popcnt(cur_msk); ctif(pre_cnt < 0); int par_diff = row_par(i, cur_msk); int row_deg_diff = row_deg[cur_msk]; for (int pre_msk = 0; pre_msk < sz; ++pre_msk) { int deg_diff = popcnt(pre_msk^cur_msk)+row_deg_diff; for (int par : {0, 1}) { pull &dst = dp[i][cur_cnt][cur_msk][par]; pull src = dp[i-1][pre_cnt][pre_msk][par^par_diff]; update(dst, src, deg_diff); } } } } } //cerr << "final row:\nmsk\tcnt\tsum\n"; for (int msk = 0; msk < sz; ++msk) { pull p = dp[n-1][stcnt][msk][tpar]; degsum += p.sec; //totcnt += p.fir; ///ctif(p.fir == 0); //cerr << msk << '\t' << p.fir << '\t' << p.sec << '\n'; } //cerr << "tdeg = " << tdeg << '\n'; //cerr << "totcnt = " << totcnt << '\n'; //cerr << "degsum = " << degsum << '\n'; cout << tdeg/degsum << '.'; tdeg %= degsum; constexpr int prec = 15; for (int i = 0; i < prec; ++i) { tdeg *= 10; cout << char(tdeg/degsum+'0'); tdeg %= degsum; } cout << '\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 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define NM 8 #define CNT 8 #define SZ (1<<NM) int tmsk[NM]; char s[NM][NM+8], t[NM][NM+8]; int row_deg[SZ]; pull dp[NM][CNT+1][SZ][2]; /// row, cnt, msk, par -> deg(cnt, sum) int row_par(int row, int msk) { return popcnt((0xaa>>(row%2))&msk)%2; } void update(pull &dst, pull src, int deg) { dst.fir += src.fir; dst.sec += src.sec+src.fir*deg; } int main() { canhazfast(); int n, m, sz, gmsk; int spar = 0, tpar = 0, stcnt = 0; ull tdeg, totcnt = 0, degsum = 0; cin >> n >> m; sz = 1<<m; gmsk = sz-1; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < m; ++j) { if (s[i][j] == 'O') { ++stcnt; spar ^= (i^j)&1; } } } for (int i = 0; i < n; ++i) { cin >> t[i]; for (int j = 0; j < m; ++j) { if (t[i][j] == 'O') { tmsk[i] |= 1<<j; tpar ^= (i^j)&1; } } } retif(spar != tpar) cout << "0\n", 0; for (int msk = 0; msk < sz; ++msk) row_deg[msk] = popcnt((msk^(msk>>1))&(gmsk>>1)); tdeg = row_deg[tmsk[0]]; for (int i = 1; i < n; ++i) { tdeg += row_deg[tmsk[i]]; tdeg += popcnt(tmsk[i-1]^tmsk[i]); } //cerr << "init:\nmsk\tpcnt\tpar\tdeg\n"; for (int msk = 0; msk < sz; ++msk) { int cnt = popcnt(msk); int par = row_par(0, msk); dp[0][cnt][msk][par] = {1, row_deg[msk]}; //cerr << bitset<5>(msk) << '\t' << cnt << '\t' << par << '\t' << row_deg[msk] << '\n'; } for (int i = 1; i < n; ++i) { for (int cur_cnt = 0; cur_cnt <= stcnt; ++cur_cnt) { for (int cur_msk = 0; cur_msk < sz; ++cur_msk) { int pre_cnt = cur_cnt-popcnt(cur_msk); ctif(pre_cnt < 0); int par_diff = row_par(i, cur_msk); int row_deg_diff = row_deg[cur_msk]; for (int pre_msk = 0; pre_msk < sz; ++pre_msk) { int deg_diff = popcnt(pre_msk^cur_msk)+row_deg_diff; for (int par : {0, 1}) { pull &dst = dp[i][cur_cnt][cur_msk][par]; pull src = dp[i-1][pre_cnt][pre_msk][par^par_diff]; update(dst, src, deg_diff); } } } } } //cerr << "final row:\nmsk\tcnt\tsum\n"; for (int msk = 0; msk < sz; ++msk) { pull p = dp[n-1][stcnt][msk][tpar]; degsum += p.sec; //totcnt += p.fir; ///ctif(p.fir == 0); //cerr << msk << '\t' << p.fir << '\t' << p.sec << '\n'; } //cerr << "tdeg = " << tdeg << '\n'; //cerr << "totcnt = " << totcnt << '\n'; //cerr << "degsum = " << degsum << '\n'; cout << tdeg/degsum << '.'; tdeg %= degsum; constexpr int prec = 15; for (int i = 0; i < prec; ++i) { tdeg *= 10; cout << char(tdeg/degsum+'0'); tdeg %= degsum; } cout << '\n'; return 0; } |