fca716a31c 2011-07-26 kinaba: #include <iostream> fca716a31c 2011-07-26 kinaba: #include <sstream> fca716a31c 2011-07-26 kinaba: #include <iomanip> fca716a31c 2011-07-26 kinaba: #include <vector> fca716a31c 2011-07-26 kinaba: #include <string> fca716a31c 2011-07-26 kinaba: #include <map> fca716a31c 2011-07-26 kinaba: #include <set> fca716a31c 2011-07-26 kinaba: #include <algorithm> fca716a31c 2011-07-26 kinaba: #include <numeric> fca716a31c 2011-07-26 kinaba: #include <iterator> fca716a31c 2011-07-26 kinaba: #include <functional> fca716a31c 2011-07-26 kinaba: #include <complex> fca716a31c 2011-07-26 kinaba: #include <queue> fca716a31c 2011-07-26 kinaba: #include <stack> fca716a31c 2011-07-26 kinaba: #include <cmath> fca716a31c 2011-07-26 kinaba: #include <cassert> fca716a31c 2011-07-26 kinaba: #include <cstring> fca716a31c 2011-07-26 kinaba: using namespace std; fca716a31c 2011-07-26 kinaba: typedef long long LL; fca716a31c 2011-07-26 kinaba: typedef complex<double> CMP; fca716a31c 2011-07-26 kinaba: fca716a31c 2011-07-26 kinaba: template<typename T> fca716a31c 2011-07-26 kinaba: struct DP2 fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: const int N1, N2; fca716a31c 2011-07-26 kinaba: vector<T> data; fca716a31c 2011-07-26 kinaba: DP2(int N1, int N2, const T& t = T()) fca716a31c 2011-07-26 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } fca716a31c 2011-07-26 kinaba: T& operator()(int i1, int i2) fca716a31c 2011-07-26 kinaba: { return data[ (i1*N2)+i2 ]; } fca716a31c 2011-07-26 kinaba: void swap(DP2& rhs) fca716a31c 2011-07-26 kinaba: { data.swap(rhs.data); } fca716a31c 2011-07-26 kinaba: }; fca716a31c 2011-07-26 kinaba: fca716a31c 2011-07-26 kinaba: class PerfectMemory { public: fca716a31c 2011-07-26 kinaba: double getExpectation(int N, int M) fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: const int C = N*M; fca716a31c 2011-07-26 kinaba: DP2<double> dp(C+1, C+1); fca716a31c 2011-07-26 kinaba: for(int u=0; u<=C; ++u) fca716a31c 2011-07-26 kinaba: for(int k=0; k<=C; ++k) if( (u+k)%2==0 && k+u<=C ) fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: if( k > u ) fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: dp(k,u) = 1 + dp(k-2,u); fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: else if( k == u ) fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: if( k==0 && u==0 ) fca716a31c 2011-07-26 kinaba: dp(k,u) = 0; fca716a31c 2011-07-26 kinaba: else fca716a31c 2011-07-26 kinaba: dp(k,u) = 1 + dp(k-1, u-1); fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: else // k < u fca716a31c 2011-07-26 kinaba: { fca716a31c 2011-07-26 kinaba: dp(k,u) = 1; fca716a31c 2011-07-26 kinaba: fca716a31c 2011-07-26 kinaba: double pFirstIsKnown = double(k) / u; fca716a31c 2011-07-26 kinaba: double pSecondIsIt = double(1) / (u-1); fca716a31c 2011-07-26 kinaba: if(k) fca716a31c 2011-07-26 kinaba: dp(k,u) += pFirstIsKnown * dp(k-1, u-1); fca716a31c 2011-07-26 kinaba: double pSecondIsThat = double(k) / (u-1); fca716a31c 2011-07-26 kinaba: dp(k,u) += (1-pFirstIsKnown) * pSecondIsIt * dp(k, u-2); fca716a31c 2011-07-26 kinaba: dp(k,u) += (1-pFirstIsKnown) * (1-pSecondIsIt-pSecondIsThat) * dp(k+2, u-2); fca716a31c 2011-07-26 kinaba: dp(k,u) += (1-pFirstIsKnown) * pSecondIsThat * (1 + dp(k, u-2)); fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: // cerr << k << " " << u << " " << dp(k,u) << endl; fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: return dp(0,C); fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: }; fca716a31c 2011-07-26 kinaba: fca716a31c 2011-07-26 kinaba: // BEGIN CUT HERE fca716a31c 2011-07-26 kinaba: #include <ctime> fca716a31c 2011-07-26 kinaba: double start_time; string timer() fca716a31c 2011-07-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } fca716a31c 2011-07-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) fca716a31c 2011-07-26 kinaba: { os << "{ "; fca716a31c 2011-07-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) fca716a31c 2011-07-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } fca716a31c 2011-07-26 kinaba: void verify_case(const double& Expected, const double& Received) { fca716a31c 2011-07-26 kinaba: bool ok = (abs(Expected - Received) < 1e-9); fca716a31c 2011-07-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; fca716a31c 2011-07-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } fca716a31c 2011-07-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); fca716a31c 2011-07-26 kinaba: #define END verify_case(_, PerfectMemory().getExpectation(N, M));} fca716a31c 2011-07-26 kinaba: int main(){ fca716a31c 2011-07-26 kinaba: fca716a31c 2011-07-26 kinaba: CASE(0) fca716a31c 2011-07-26 kinaba: int N = 1; fca716a31c 2011-07-26 kinaba: int M = 2; fca716a31c 2011-07-26 kinaba: double _ = 1.0; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: CASE(1) fca716a31c 2011-07-26 kinaba: int N = 2; fca716a31c 2011-07-26 kinaba: int M = 2; fca716a31c 2011-07-26 kinaba: double _ = 2.6666666666666665; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: CASE(2) fca716a31c 2011-07-26 kinaba: int N = 2; fca716a31c 2011-07-26 kinaba: int M = 3; fca716a31c 2011-07-26 kinaba: double _ = 4.333333333333334; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: CASE(3) fca716a31c 2011-07-26 kinaba: int N = 4; fca716a31c 2011-07-26 kinaba: int M = 4; fca716a31c 2011-07-26 kinaba: double _ = 12.392984792984793; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: /* fca716a31c 2011-07-26 kinaba: CASE(4) fca716a31c 2011-07-26 kinaba: int N = 50; fca716a31c 2011-07-26 kinaba: int M = 50; fca716a31c 2011-07-26 kinaba: double _ = -1; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: CASE(5) fca716a31c 2011-07-26 kinaba: int N = 2; fca716a31c 2011-07-26 kinaba: int M = 1; fca716a31c 2011-07-26 kinaba: double _ = 1.0; fca716a31c 2011-07-26 kinaba: END fca716a31c 2011-07-26 kinaba: */ fca716a31c 2011-07-26 kinaba: } fca716a31c 2011-07-26 kinaba: // END CUT HERE