d6047a481e 2014-07-05 kinaba: #include <iostream> d6047a481e 2014-07-05 kinaba: #include <sstream> d6047a481e 2014-07-05 kinaba: #include <iomanip> d6047a481e 2014-07-05 kinaba: #include <vector> d6047a481e 2014-07-05 kinaba: #include <string> d6047a481e 2014-07-05 kinaba: #include <map> d6047a481e 2014-07-05 kinaba: #include <set> d6047a481e 2014-07-05 kinaba: #include <algorithm> d6047a481e 2014-07-05 kinaba: #include <numeric> d6047a481e 2014-07-05 kinaba: #include <iterator> d6047a481e 2014-07-05 kinaba: #include <functional> d6047a481e 2014-07-05 kinaba: #include <complex> d6047a481e 2014-07-05 kinaba: #include <queue> d6047a481e 2014-07-05 kinaba: #include <stack> d6047a481e 2014-07-05 kinaba: #include <cmath> d6047a481e 2014-07-05 kinaba: #include <cassert> d6047a481e 2014-07-05 kinaba: #include <tuple> d6047a481e 2014-07-05 kinaba: using namespace std; d6047a481e 2014-07-05 kinaba: typedef long long LL; d6047a481e 2014-07-05 kinaba: typedef complex<double> CMP; d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: class FixedDiceGameDiv1 { public: d6047a481e 2014-07-05 kinaba: double getExpectation(int a, int b, int c, int d) d6047a481e 2014-07-05 kinaba: { d6047a481e 2014-07-05 kinaba: if(a*b<=c*1) d6047a481e 2014-07-05 kinaba: return -1.0; d6047a481e 2014-07-05 kinaba: auto d1 = distr(a, b); d6047a481e 2014-07-05 kinaba: auto d2 = distr(c, d); d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: double total_prob = 0.0; d6047a481e 2014-07-05 kinaba: for(auto& vp: d1) d6047a481e 2014-07-05 kinaba: for(auto& zq: d2) d6047a481e 2014-07-05 kinaba: { d6047a481e 2014-07-05 kinaba: int v = vp.first; d6047a481e 2014-07-05 kinaba: double p = vp.second; d6047a481e 2014-07-05 kinaba: int z = zq.first; d6047a481e 2014-07-05 kinaba: double q = zq.second; d6047a481e 2014-07-05 kinaba: if(v > z) d6047a481e 2014-07-05 kinaba: total_prob += p*q; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: double e = 0.0; d6047a481e 2014-07-05 kinaba: for(auto& vp: d1) d6047a481e 2014-07-05 kinaba: for(auto& zq: d2) d6047a481e 2014-07-05 kinaba: { d6047a481e 2014-07-05 kinaba: int v = vp.first; d6047a481e 2014-07-05 kinaba: double p = vp.second; d6047a481e 2014-07-05 kinaba: int z = zq.first; d6047a481e 2014-07-05 kinaba: double q = zq.second; d6047a481e 2014-07-05 kinaba: if(v > z) d6047a481e 2014-07-05 kinaba: e += v * p*q/total_prob; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: return e; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: map<int, double> distr(int n, int f) d6047a481e 2014-07-05 kinaba: { d6047a481e 2014-07-05 kinaba: if(n == 0) { d6047a481e 2014-07-05 kinaba: map<int, double> m; d6047a481e 2014-07-05 kinaba: m[0] = 1,0; d6047a481e 2014-07-05 kinaba: return m; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: map<int, double> m = distr(n-1, f), m2; d6047a481e 2014-07-05 kinaba: for(auto& up: m) { d6047a481e 2014-07-05 kinaba: int u = up.first; d6047a481e 2014-07-05 kinaba: double p = up.second; d6047a481e 2014-07-05 kinaba: for(int v=1; v<=f; ++v) d6047a481e 2014-07-05 kinaba: m2[u+v] += p/f; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: return m2; d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: }; d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: // BEGIN CUT HERE d6047a481e 2014-07-05 kinaba: #include <ctime> d6047a481e 2014-07-05 kinaba: double start_time; string timer() d6047a481e 2014-07-05 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d6047a481e 2014-07-05 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d6047a481e 2014-07-05 kinaba: { os << "{ "; d6047a481e 2014-07-05 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d6047a481e 2014-07-05 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d6047a481e 2014-07-05 kinaba: void verify_case(const double& Expected, const double& Received) { d6047a481e 2014-07-05 kinaba: bool ok = (abs(Expected - Received) < 1e-9); d6047a481e 2014-07-05 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d6047a481e 2014-07-05 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d6047a481e 2014-07-05 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d6047a481e 2014-07-05 kinaba: #define END verify_case(_, FixedDiceGameDiv1().getExpectation(a, b, c, d));} d6047a481e 2014-07-05 kinaba: int main(){ d6047a481e 2014-07-05 kinaba: d6047a481e 2014-07-05 kinaba: CASE(0) d6047a481e 2014-07-05 kinaba: int a = 1; d6047a481e 2014-07-05 kinaba: int b = 2; d6047a481e 2014-07-05 kinaba: int c = 1; d6047a481e 2014-07-05 kinaba: int d = 5; d6047a481e 2014-07-05 kinaba: double _ = 2.0; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: CASE(1) d6047a481e 2014-07-05 kinaba: int a = 3; d6047a481e 2014-07-05 kinaba: int b = 1; d6047a481e 2014-07-05 kinaba: int c = 1; d6047a481e 2014-07-05 kinaba: int d = 3; d6047a481e 2014-07-05 kinaba: double _ = 3.0; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: CASE(2) d6047a481e 2014-07-05 kinaba: int a = 1; d6047a481e 2014-07-05 kinaba: int b = 5; d6047a481e 2014-07-05 kinaba: int c = 1; d6047a481e 2014-07-05 kinaba: int d = 1; d6047a481e 2014-07-05 kinaba: double _ = 3.4999999999999996; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: CASE(3) d6047a481e 2014-07-05 kinaba: int a = 2; d6047a481e 2014-07-05 kinaba: int b = 6; d6047a481e 2014-07-05 kinaba: int c = 50; d6047a481e 2014-07-05 kinaba: int d = 30; d6047a481e 2014-07-05 kinaba: double _ = -1.0; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: CASE(4) d6047a481e 2014-07-05 kinaba: int a = 50; d6047a481e 2014-07-05 kinaba: int b = 11; d6047a481e 2014-07-05 kinaba: int c = 50; d6047a481e 2014-07-05 kinaba: int d = 50; d6047a481e 2014-07-05 kinaba: double _ = 369.8865999182022; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: /* d6047a481e 2014-07-05 kinaba: CASE(5) d6047a481e 2014-07-05 kinaba: int a = ; d6047a481e 2014-07-05 kinaba: int b = ; d6047a481e 2014-07-05 kinaba: int c = ; d6047a481e 2014-07-05 kinaba: int d = ; d6047a481e 2014-07-05 kinaba: double _ = ; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: CASE(6) d6047a481e 2014-07-05 kinaba: int a = ; d6047a481e 2014-07-05 kinaba: int b = ; d6047a481e 2014-07-05 kinaba: int c = ; d6047a481e 2014-07-05 kinaba: int d = ; d6047a481e 2014-07-05 kinaba: double _ = ; d6047a481e 2014-07-05 kinaba: END d6047a481e 2014-07-05 kinaba: */ d6047a481e 2014-07-05 kinaba: } d6047a481e 2014-07-05 kinaba: // END CUT HERE