4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class KthProbableElement { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double probability(int M, int lowerBound, int upperBound, int N, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double dp1[101][101]={0}, dp2[101][101], (*P)[101]=dp1, (*Q)[101]=dp2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: P[0][0] = 1.0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<M; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: memset( Q, 0, sizeof(double)*101*101 ); 4fd800b3a8 2011-02-23 kinaba: for(int a=0; a<=i; ++a) 4fd800b3a8 2011-02-23 kinaba: for(int b=0; a+b<=i; ++b) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: Q[a+1][b] += P[a][b] * (N-lowerBound) / (upperBound-lowerBound+1); 4fd800b3a8 2011-02-23 kinaba: Q[a][b+1] += P[a][b] * 1 / (upperBound-lowerBound+1); 4fd800b3a8 2011-02-23 kinaba: Q[a][b] += P[a][b] * (upperBound-N) / (upperBound-lowerBound+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: swap(P, Q); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double p = 0.0; 4fd800b3a8 2011-02-23 kinaba: for(int a=0; a<=M; ++a) 4fd800b3a8 2011-02-23 kinaba: for(int b=0; a+b<=M; ++b) 4fd800b3a8 2011-02-23 kinaba: if( a<K && K<=a+b ) 4fd800b3a8 2011-02-23 kinaba: p += P[a][b]; 4fd800b3a8 2011-02-23 kinaba: return p; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int M = 1; 4fd800b3a8 2011-02-23 kinaba: int lowerBound = 1; 4fd800b3a8 2011-02-23 kinaba: int upperBound = 10; 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upperBound, N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int M = 3; 4fd800b3a8 2011-02-23 kinaba: int lowerBound = 1; 4fd800b3a8 2011-02-23 kinaba: int upperBound = 2; 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upperBound, N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int M = 3; 4fd800b3a8 2011-02-23 kinaba: int lowerBound = 1; 4fd800b3a8 2011-02-23 kinaba: int upperBound = 3; 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.48148148148148145; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upperBound, N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int M = 10; 4fd800b3a8 2011-02-23 kinaba: int lowerBound = 1; 4fd800b3a8 2011-02-23 kinaba: int upperBound = 10; 4fd800b3a8 2011-02-23 kinaba: int N = 1; 4fd800b3a8 2011-02-23 kinaba: int K = 10; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.0000000000000003E-10; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upperBound, N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int M = 4; 4fd800b3a8 2011-02-23 kinaba: int lowerBound = 61; 4fd800b3a8 2011-02-23 kinaba: int upperBound = 65; 4fd800b3a8 2011-02-23 kinaba: int N = 62; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.15200000000000002; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upperBound, N, K)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: