76db68d634 2018-07-28 kinaba: #include <iostream> 76db68d634 2018-07-28 kinaba: #include <sstream> 76db68d634 2018-07-28 kinaba: #include <iomanip> 76db68d634 2018-07-28 kinaba: #include <vector> 76db68d634 2018-07-28 kinaba: #include <string> 76db68d634 2018-07-28 kinaba: #include <map> 76db68d634 2018-07-28 kinaba: #include <set> 76db68d634 2018-07-28 kinaba: #include <algorithm> 76db68d634 2018-07-28 kinaba: #include <numeric> 76db68d634 2018-07-28 kinaba: #include <iterator> 76db68d634 2018-07-28 kinaba: #include <functional> 76db68d634 2018-07-28 kinaba: #include <complex> 76db68d634 2018-07-28 kinaba: #include <queue> 76db68d634 2018-07-28 kinaba: #include <stack> 76db68d634 2018-07-28 kinaba: #include <cmath> 76db68d634 2018-07-28 kinaba: #include <cassert> 76db68d634 2018-07-28 kinaba: #include <tuple> 76db68d634 2018-07-28 kinaba: using namespace std; 76db68d634 2018-07-28 kinaba: typedef long long LL; 76db68d634 2018-07-28 kinaba: typedef complex<double> CMP; 76db68d634 2018-07-28 kinaba: 76db68d634 2018-07-28 kinaba: class TestProctoring { public: 76db68d634 2018-07-28 kinaba: double expectedTime(vector <int> p, vector <int> q) 76db68d634 2018-07-28 kinaba: { 76db68d634 2018-07-28 kinaba: const int N = p.size(); 76db68d634 2018-07-28 kinaba: vector<double> dp(1<<N), dp2((N+1)*(1<<N)); 76db68d634 2018-07-28 kinaba: for (int mask = 1; mask < (1<<N); ++mask) { 76db68d634 2018-07-28 kinaba: for (int i = N; i >= 0; --i) 76db68d634 2018-07-28 kinaba: if (i == N) 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = dp[mask]; 76db68d634 2018-07-28 kinaba: else if ((mask&(1 << i)) == 0) 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = dp2[((i + 1) << N) | mask]; 76db68d634 2018-07-28 kinaba: else { 76db68d634 2018-07-28 kinaba: double e1 = dp2[((i + 1) << N) | mask &~(1 << i)]; 76db68d634 2018-07-28 kinaba: double e2 = dp2[((i + 1) << N) | mask]; 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = (p[i] * e1 + (q[i] - p[i])*e2) / q[i]; 76db68d634 2018-07-28 kinaba: } 76db68d634 2018-07-28 kinaba: 76db68d634 2018-07-28 kinaba: double pp = 1.0; 76db68d634 2018-07-28 kinaba: for (int i = 0; i<N; ++i) if (mask&(1 << i)) 76db68d634 2018-07-28 kinaba: pp = pp * (q[i] - p[i]) / q[i]; 76db68d634 2018-07-28 kinaba: dp[mask] = (dp2[mask]+1) / (1 - pp); 76db68d634 2018-07-28 kinaba: 76db68d634 2018-07-28 kinaba: // Rerun 76db68d634 2018-07-28 kinaba: for (int i = N; i >= 0; --i) 76db68d634 2018-07-28 kinaba: if (i == N) 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = dp[mask]; 76db68d634 2018-07-28 kinaba: else if ((mask&(1 << i)) == 0) 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = dp2[((i + 1) << N) | mask]; 76db68d634 2018-07-28 kinaba: else { 76db68d634 2018-07-28 kinaba: double e1 = dp2[((i + 1) << N) | mask &~(1 << i)]; 76db68d634 2018-07-28 kinaba: double e2 = dp2[((i + 1) << N) | mask]; 76db68d634 2018-07-28 kinaba: dp2[(i << N) | mask] = (p[i] * e1 + (q[i] - p[i])*e2) / q[i]; 76db68d634 2018-07-28 kinaba: } 76db68d634 2018-07-28 kinaba: } 76db68d634 2018-07-28 kinaba: 76db68d634 2018-07-28 kinaba: return dp[(1<<N) - 1]; 76db68d634 2018-07-28 kinaba: } 76db68d634 2018-07-28 kinaba: }; 76db68d634 2018-07-28 kinaba: 76db68d634 2018-07-28 kinaba: // BEGIN CUT HERE 76db68d634 2018-07-28 kinaba: #include <ctime> 76db68d634 2018-07-28 kinaba: double start_time; string timer() 76db68d634 2018-07-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 76db68d634 2018-07-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 76db68d634 2018-07-28 kinaba: { os << "{ "; 76db68d634 2018-07-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 76db68d634 2018-07-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 76db68d634 2018-07-28 kinaba: void verify_case(const double& Expected, const double& Received) { 76db68d634 2018-07-28 kinaba: bool ok = (abs(Expected - Received)/abs(Expected) < 1e-6); 76db68d634 2018-07-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 76db68d634 2018-07-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 76db68d634 2018-07-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 76db68d634 2018-07-28 kinaba: #define END verify_case(_, TestProctoring().expectedTime(p, q));} 76db68d634 2018-07-28 kinaba: int main(){ 76db68d634 2018-07-28 kinaba: CASE(0) 76db68d634 2018-07-28 kinaba: int p_[] = {2}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {4}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 2.0; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(1) 76db68d634 2018-07-28 kinaba: int p_[] = {1,2}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {2,4}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 2.6666666666666665; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(2) 76db68d634 2018-07-28 kinaba: int p_[] = {3,1,2,4,2,5}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {3,1,2,4,2,5}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 1.0; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(3) 76db68d634 2018-07-28 kinaba: int p_[] = {1,1,1,1,1,1,1,1}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {1,2,3,4,5,6,7,8}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 13.604834665120991; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(4) 76db68d634 2018-07-28 kinaba: int p_[] = {2,3,5,7,11,13,17}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {3,5,7,11,13,17,19}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 2.6299445065924276; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(5) 76db68d634 2018-07-28 kinaba: int p_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = {1000000000,1000000000,1000000000,1000000000,1000000000, 76db68d634 2018-07-28 kinaba: 1000000000,1000000000,1000000000,1000000000,1000000000, 76db68d634 2018-07-28 kinaba: 1000000000,1000000000,1000000000,1000000000,1000000000, 76db68d634 2018-07-28 kinaba: 1000000000,1000000000,1000000000,1000000000,1000000000}; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = 3.597740924491638E9; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: /* 76db68d634 2018-07-28 kinaba: CASE(6) 76db68d634 2018-07-28 kinaba: int p_[] = ; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = ; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = ; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: CASE(7) 76db68d634 2018-07-28 kinaba: int p_[] = ; 76db68d634 2018-07-28 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 76db68d634 2018-07-28 kinaba: int q_[] = ; 76db68d634 2018-07-28 kinaba: vector <int> q(q_, q_+sizeof(q_)/sizeof(*q_)); 76db68d634 2018-07-28 kinaba: double _ = ; 76db68d634 2018-07-28 kinaba: END 76db68d634 2018-07-28 kinaba: */ 76db68d634 2018-07-28 kinaba: } 76db68d634 2018-07-28 kinaba: // END CUT HERE