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