1535d63cea 2011-05-28 kinaba: #include <iostream> 1535d63cea 2011-05-28 kinaba: #include <sstream> 1535d63cea 2011-05-28 kinaba: #include <iomanip> 1535d63cea 2011-05-28 kinaba: #include <vector> 1535d63cea 2011-05-28 kinaba: #include <string> 1535d63cea 2011-05-28 kinaba: #include <map> 1535d63cea 2011-05-28 kinaba: #include <set> 1535d63cea 2011-05-28 kinaba: #include <algorithm> 1535d63cea 2011-05-28 kinaba: #include <numeric> 1535d63cea 2011-05-28 kinaba: #include <iterator> 1535d63cea 2011-05-28 kinaba: #include <functional> 1535d63cea 2011-05-28 kinaba: #include <complex> 1535d63cea 2011-05-28 kinaba: #include <queue> 1535d63cea 2011-05-28 kinaba: #include <stack> 1535d63cea 2011-05-28 kinaba: #include <cmath> 1535d63cea 2011-05-28 kinaba: #include <cassert> 1535d63cea 2011-05-28 kinaba: #include <cstring> 1535d63cea 2011-05-28 kinaba: using namespace std; 1535d63cea 2011-05-28 kinaba: typedef long long LL; 1535d63cea 2011-05-28 kinaba: typedef complex<double> CMP; 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: class TheTicketsDivOne { public: 1535d63cea 2011-05-28 kinaba: double find(int n, int m) 1535d63cea 2011-05-28 kinaba: { 1535d63cea 2011-05-28 kinaba: vector<double> p(1, 1.0); 1535d63cea 2011-05-28 kinaba: for(int N=2; N<=n; ++N) { 1535d63cea 2011-05-28 kinaba: vector<double> q(N, 0.0); 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: // q[0] = 1/6 + 1/2 q[N-1] 1535d63cea 2011-05-28 kinaba: // q[i] = 1/2 q[i-1] + 1/3 p[i-1] 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: double s = 1.0/6; 1535d63cea 2011-05-28 kinaba: double f = 1.0/3; 1535d63cea 2011-05-28 kinaba: for(int i=N-2; i>=0; --i) { 1535d63cea 2011-05-28 kinaba: f = f/2; 1535d63cea 2011-05-28 kinaba: s += f*p[i]; 1535d63cea 2011-05-28 kinaba: } 1535d63cea 2011-05-28 kinaba: q[0] = s / (1.0 - pow(0.5, N)); 1535d63cea 2011-05-28 kinaba: for(int i=1; i<N; ++i) 1535d63cea 2011-05-28 kinaba: q[i] = q[i-1]/2.0 + p[i-1]/3.0; 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: p.swap(q); 1535d63cea 2011-05-28 kinaba: } 1535d63cea 2011-05-28 kinaba: return p[m-1]; 1535d63cea 2011-05-28 kinaba: } 1535d63cea 2011-05-28 kinaba: }; 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: // BEGIN CUT HERE 1535d63cea 2011-05-28 kinaba: #include <ctime> 1535d63cea 2011-05-28 kinaba: double start_time; string timer() 1535d63cea 2011-05-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 1535d63cea 2011-05-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 1535d63cea 2011-05-28 kinaba: { os << "{ "; 1535d63cea 2011-05-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 1535d63cea 2011-05-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 1535d63cea 2011-05-28 kinaba: void verify_case(const double& Expected, const double& Received) { 1535d63cea 2011-05-28 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 1535d63cea 2011-05-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 1535d63cea 2011-05-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 1535d63cea 2011-05-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 1535d63cea 2011-05-28 kinaba: #define END verify_case(_, TheTicketsDivOne().find(n, m));} 1535d63cea 2011-05-28 kinaba: int main(){ 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: CASE(0) 1535d63cea 2011-05-28 kinaba: int n = 2; 1535d63cea 2011-05-28 kinaba: int m = 1; 1535d63cea 2011-05-28 kinaba: double _ = 0.4444444444444444; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(1) 1535d63cea 2011-05-28 kinaba: int n = 2; 1535d63cea 2011-05-28 kinaba: int m = 2; 1535d63cea 2011-05-28 kinaba: double _ = 0.5555555555555556; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(2) 1535d63cea 2011-05-28 kinaba: int n = 1; 1535d63cea 2011-05-28 kinaba: int m = 1; 1535d63cea 2011-05-28 kinaba: double _ = 1.0; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(3) 1535d63cea 2011-05-28 kinaba: int n = 3; 1535d63cea 2011-05-28 kinaba: int m = 2; 1535d63cea 2011-05-28 kinaba: double _ = 0.31746031746031744; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(4) 1535d63cea 2011-05-28 kinaba: int n = 1000; 1535d63cea 2011-05-28 kinaba: int m = 500; 1535d63cea 2011-05-28 kinaba: double _ = -1; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(5) 1535d63cea 2011-05-28 kinaba: int n = 1; 1535d63cea 2011-05-28 kinaba: int m = 1; 1535d63cea 2011-05-28 kinaba: double _ = 1.0; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(6) 1535d63cea 2011-05-28 kinaba: int n = 1000; 1535d63cea 2011-05-28 kinaba: int m = 1000; 1535d63cea 2011-05-28 kinaba: double _ = -1; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: CASE(7) 1535d63cea 2011-05-28 kinaba: int n = 1000; 1535d63cea 2011-05-28 kinaba: int m = 1; 1535d63cea 2011-05-28 kinaba: double _ = -1; 1535d63cea 2011-05-28 kinaba: END 1535d63cea 2011-05-28 kinaba: 1535d63cea 2011-05-28 kinaba: } 1535d63cea 2011-05-28 kinaba: // END CUT HERE