Artifact Content
Not logged in

Artifact 4ab9d94a2abea13a2c7703a4bbc4ba9430731547


#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 BouncingDiceGame { public:
	double winProbability(int n, int d, int x, int y) 
	{
		int B = n-d;
		x = min(B,x);
		y = min(B,y);
		int Z = min(x,y);
		vector< vector<double> > p(B+1, vector<double>(B+1, 0));

		// solve:
		//   p[B][k] = 1 - p[k][B]
		//   p[i][k] = 1/d \Sigma_{b=1..d} (1 - p[k][min(B,i+b)])
		// return p[x][y];

		for(int ik=B*2; ik>=Z*2; --ik)
		for(int i=Z; i<=B; ++i)
		{
			int k = ik-i;
			if( Z<=k && k<=B ) {
				if(i==B && k==B) p[B][B] = d / (2*d-1);
				else if( i==B ) p[B][k] = 1 - p[k][B]*(d-1)/d;
				else {
					double q = 0;
					for(int b=1; b<=d; ++b)
						q += 1 - p[k][min(B,i+b)];
					p[i][k] = q/d;
				}
			}
		}
		return p[x][y];
	}
};

// 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) {
 bool ok = (abs(Expected - Received) < 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(_, BouncingDiceGame().winProbability(n, d, x, y));}
int main(){

CASE(0)
	int n = 10; 
	int d = 6; 
	int x = 1; 
	int y = 1; 
	double _ = 0.5417251215862328; 
END
CASE(1)
	int n = 10; 
	int d = 2; 
	int x = 1; 
	int y = 1; 
	double _ = 0.6090494791666666; 
END
CASE(2)
	int n = 100; 
	int d = 20; 
	int x = 1; 
	int y = 10; 
	double _ = 0.49158887163174947; 
END
CASE(3)
	int n = 10; 
	int d = 5; 
	int x = 9; 
	int y = 1; 
	double _ = 0.6943018666666667; 
END
/*
CASE(4)
	int n = ; 
	int d = ; 
	int x = ; 
	int y = ; 
	double _ = ; 
END
CASE(5)
	int n = ; 
	int d = ; 
	int x = ; 
	int y = ; 
	double _ = ; 
END
*/
}
// END CUT HERE