4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class LastMarble { public: 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,int> rb; 4fd800b3a8 2011-02-23 kinaba: typedef map<rb,double> state; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double winningChance(int red, int blue, int removed) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: state s; 4fd800b3a8 2011-02-23 kinaba: addNextStates( red, blue, 1.0, removed, s ); 4fd800b3a8 2011-02-23 kinaba: return winChance(s); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double winChance( state& s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( s.empty() ) 4fd800b3a8 2011-02-23 kinaba: return 1.0; // any value is ok because it will be multiplied by 0.0 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int r_plus_b = s.begin()->first.first + s.begin()->first.second; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double lc = 1.0; 4fd800b3a8 2011-02-23 kinaba: for(int take=1; take<=min(3,r_plus_b); ++take) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: state ns; 4fd800b3a8 2011-02-23 kinaba: for(state::iterator it=s.begin(); it!=s.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: addNextStates(it->first.first, it->first.second, it->second, take, ns); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double nowLoseChance = 0.0; 4fd800b3a8 2011-02-23 kinaba: for(state::iterator it=ns.begin(); it!=ns.end(); ) 4fd800b3a8 2011-02-23 kinaba: if( it->first.first == 0 ) { 4fd800b3a8 2011-02-23 kinaba: nowLoseChance += it->second; 4fd800b3a8 2011-02-23 kinaba: ns.erase( it++ ); 4fd800b3a8 2011-02-23 kinaba: } else { 4fd800b3a8 2011-02-23 kinaba: ++it; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double continueChance = 1.0 - nowLoseChance; 4fd800b3a8 2011-02-23 kinaba: for(state::iterator it=ns.begin(); it!=ns.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: it->second /= continueChance; 4fd800b3a8 2011-02-23 kinaba: double loseChance = nowLoseChance + winChance(ns)*continueChance; 4fd800b3a8 2011-02-23 kinaba: lc = min(lc, loseChance); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return 1.0 - lc; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void addNextStates(int r, int b, double p, int take, state& m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( take == 0 ) { 4fd800b3a8 2011-02-23 kinaba: m[rb(r,b)] += p; 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: state tmp; 4fd800b3a8 2011-02-23 kinaba: tmp[rb(r,b)] = p; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: while( take --> 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: state tmp2; 4fd800b3a8 2011-02-23 kinaba: state& tgt = (take ? tmp2 : m); 4fd800b3a8 2011-02-23 kinaba: for(state::iterator it=tmp.begin(); it!=tmp.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int r = it->first.first; 4fd800b3a8 2011-02-23 kinaba: int b = it->first.second; 4fd800b3a8 2011-02-23 kinaba: double p = it->second; 4fd800b3a8 2011-02-23 kinaba: if( r>0 ) tgt[rb(r-1,b)] += p * r / (r+b); 4fd800b3a8 2011-02-23 kinaba: if( b>0 ) tgt[rb(r,b-1)] += p * b / (r+b); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: tmp.swap(tmp2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const double& Expected, const double& Received) { 4fd800b3a8 2011-02-23 kinaba: double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 1e-9); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, LastMarble().winningChance(red, blue, removed));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int red = 1; 4fd800b3a8 2011-02-23 kinaba: int blue = 1; 4fd800b3a8 2011-02-23 kinaba: int removed = 0; 4fd800b3a8 2011-02-23 kinaba: double _ = 0.5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int red = 1; 4fd800b3a8 2011-02-23 kinaba: int blue = 2; 4fd800b3a8 2011-02-23 kinaba: int removed = 0; 4fd800b3a8 2011-02-23 kinaba: double _ = 0.3333333333333333; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int red = 2; 4fd800b3a8 2011-02-23 kinaba: int blue = 1; 4fd800b3a8 2011-02-23 kinaba: int removed = 0; 4fd800b3a8 2011-02-23 kinaba: double _ = 0.6666666666666666; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int red = 2; 4fd800b3a8 2011-02-23 kinaba: int blue = 2; 4fd800b3a8 2011-02-23 kinaba: int removed = 1; 4fd800b3a8 2011-02-23 kinaba: double _ = 0.5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int red = ; 4fd800b3a8 2011-02-23 kinaba: int blue = ; 4fd800b3a8 2011-02-23 kinaba: int removed = ; 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int red = ; 4fd800b3a8 2011-02-23 kinaba: int blue = ; 4fd800b3a8 2011-02-23 kinaba: int removed = ; 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE