Artifact Content
Not logged in

Artifact 48b50023e1108913a83b4121a32f69c6092b5bcc


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TestProctoring { public:
	double expectedTime(vector <int> p, vector <int> q)
	{
		const int N = p.size();
		vector<double> dp(1<<N), dp2((N+1)*(1<<N));
		for (int mask = 1; mask < (1<<N); ++mask) {
			for (int i = N; i >= 0; --i)
				if (i == N)
					dp2[(i << N) | mask] = dp[mask];
				else if ((mask&(1 << i)) == 0)
					dp2[(i << N) | mask] = dp2[((i + 1) << N) | mask];
				else {
					double e1 = dp2[((i + 1) << N) | mask &~(1 << i)];
					double e2 = dp2[((i + 1) << N) | mask];
					dp2[(i << N) | mask] = (p[i] * e1 + (q[i] - p[i])*e2) / q[i];
				}

			double pp = 1.0;
			for (int i = 0; i<N; ++i) if (mask&(1 << i))
				pp = pp * (q[i] - p[i]) / q[i];
			dp[mask] = (dp2[mask]+1) / (1 - pp);

			// Rerun
			for (int i = N; i >= 0; --i)
				if (i == N)
					dp2[(i << N) | mask] = dp[mask];
				else if ((mask&(1 << i)) == 0)
					dp2[(i << N) | mask] = dp2[((i + 1) << N) | mask];
				else {
					double e1 = dp2[((i + 1) << N) | mask &~(1 << i)];
					double e2 = dp2[((i + 1) << N) | mask];
					dp2[(i << N) | mask] = (p[i] * e1 + (q[i] - p[i])*e2) / q[i];
				}
		}

		return dp[(1<<N) - 1];
	}
};

// 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)/abs(Expected) < 1e-6);
 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(_, TestProctoring().expectedTime(p, q));}
int main(){
CASE(0)
	int p_[] = {2};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {4};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 2.0; 
END
CASE(1)
	int p_[] = {1,2};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {2,4};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 2.6666666666666665; 
END
CASE(2)
	int p_[] = {3,1,2,4,2,5};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {3,1,2,4,2,5};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 1.0; 
END
CASE(3)
	int p_[] = {1,1,1,1,1,1,1,1};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {1,2,3,4,5,6,7,8};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 13.604834665120991; 
END
CASE(4)
	int p_[] = {2,3,5,7,11,13,17};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {3,5,7,11,13,17,19};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 2.6299445065924276; 
END
CASE(5)
	int p_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = {1000000000,1000000000,1000000000,1000000000,1000000000,
1000000000,1000000000,1000000000,1000000000,1000000000,
1000000000,1000000000,1000000000,1000000000,1000000000,
1000000000,1000000000,1000000000,1000000000,1000000000};
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = 3.597740924491638E9; 
END
/*
CASE(6)
	int p_[] = ;
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = ;
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = ; 
END
CASE(7)
	int p_[] = ;
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int q_[] = ;
	  vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 
	double _ = ; 
END
*/
}
// END CUT HERE