//Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) 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<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; typedef long double ld; struct rt { ll p, q; }; rt norm(rt a) { ll gg = __gcd(a.p, a.q); rt tmp = rt({a.p / gg, a.q / gg}); if(tmp.p < 0) { tmp.p = -tmp.p; tmp.q = -tmp.q; } return tmp; } rt operator * (rt a, rt b) { return norm(rt({a.p * b.p, a.q * b.q})); } rt operator + (rt a, rt b) { return norm(rt{a.p * b.q + b.p * a.q, a.q * b.q}); } rt operator - (rt a, rt b) { return norm(rt{a.p * b.q - b.p * a.q, a.q * b.q}); } void n1(int m) { string s; cin >> s; rt ow = {0LL, 1LL}; rt ok = ow; rt od = ow; auto eval = [&] (int x) { if(x < 0 || x >= m) return rt({0LL, 1LL}); char c = s[x]; if(c == '.') return rt({0LL, 1LL}); else if(c == 'O') return rt({1LL, 1LL}); else return rt({1LL, 2LL}); }; auto ne = [&] (rt x) { return (rt({1LL, 1LL}) - x); }; REP(i, m) { ow = ow + eval(i); ok = ok + eval(i) * eval(i - 1); od = od + ne(eval(i - 1)) * eval(i) * eval(i + 1) * ne(eval(i + 2)); } rt w = ow - ok - od; cout << w.p << "/" << w.q << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; if(n == 1) { n1(m); 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 | //Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) 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<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; typedef long double ld; struct rt { ll p, q; }; rt norm(rt a) { ll gg = __gcd(a.p, a.q); rt tmp = rt({a.p / gg, a.q / gg}); if(tmp.p < 0) { tmp.p = -tmp.p; tmp.q = -tmp.q; } return tmp; } rt operator * (rt a, rt b) { return norm(rt({a.p * b.p, a.q * b.q})); } rt operator + (rt a, rt b) { return norm(rt{a.p * b.q + b.p * a.q, a.q * b.q}); } rt operator - (rt a, rt b) { return norm(rt{a.p * b.q - b.p * a.q, a.q * b.q}); } void n1(int m) { string s; cin >> s; rt ow = {0LL, 1LL}; rt ok = ow; rt od = ow; auto eval = [&] (int x) { if(x < 0 || x >= m) return rt({0LL, 1LL}); char c = s[x]; if(c == '.') return rt({0LL, 1LL}); else if(c == 'O') return rt({1LL, 1LL}); else return rt({1LL, 2LL}); }; auto ne = [&] (rt x) { return (rt({1LL, 1LL}) - x); }; REP(i, m) { ow = ow + eval(i); ok = ok + eval(i) * eval(i - 1); od = od + ne(eval(i - 1)) * eval(i) * eval(i + 1) * ne(eval(i + 2)); } rt w = ow - ok - od; cout << w.p << "/" << w.q << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; if(n == 1) { n1(m); return 0; } } |