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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double f(int a, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double x = 1; 4fd800b3a8 2011-02-23 kinaba: while( n --> 0 ) 4fd800b3a8 2011-02-23 kinaba: x *= a--; 4fd800b3a8 2011-02-23 kinaba: return x; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: double f(int a) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return f(a, a); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: double C(int n, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return f(n,k) / f(k,k); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Flush 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double size(vector <int> suits, int number) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double all = f(accumulate(suits.begin(), suits.end(), 0), number); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i1=0; i1<=number && i1<=suits[0]; ++i1) 4fd800b3a8 2011-02-23 kinaba: for(int i2=0; i1+i2<=number && i2<=suits[1]; ++i2) 4fd800b3a8 2011-02-23 kinaba: for(int i3=0; i1+i2+i3<=number && i3<=suits[2]; ++i3) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int i4 = number - i1 - i2 - i3; 4fd800b3a8 2011-02-23 kinaba: if( i4 <= suits[3] ) { 4fd800b3a8 2011-02-23 kinaba: double nf = max(max(i1,i2),max(i3,i4)); 4fd800b3a8 2011-02-23 kinaba: double n = C(number, i1) * C(number-i1, i2) * C(number-i1-i2, i3) 4fd800b3a8 2011-02-23 kinaba: * f(suits[0],i1) * f(suits[1],i2) * f(suits[2],i3) * f(suits[3],i4); 4fd800b3a8 2011-02-23 kinaba: ans += nf * n/all; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 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 suits_[] = {2,2,2,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); 4fd800b3a8 2011-02-23 kinaba: int number = 2; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.1428571428571428; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Flush().size(suits, number)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int suits_[] = {1,4,7,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); 4fd800b3a8 2011-02-23 kinaba: int number = 22; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 10.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Flush().size(suits, number)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int suits_[] = {13, 13, 13, 13}; 4fd800b3a8 2011-02-23 kinaba: vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); 4fd800b3a8 2011-02-23 kinaba: int number = 49; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 13.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Flush().size(suits, number)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int suits_[] = {13, 13, 13, 13}; 4fd800b3a8 2011-02-23 kinaba: vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); 4fd800b3a8 2011-02-23 kinaba: int number = 26; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 8.351195960938014; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Flush().size(suits, number)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int suits_[] = {13,13,13,13}; 4fd800b3a8 2011-02-23 kinaba: vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); 4fd800b3a8 2011-02-23 kinaba: int number = 0; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Flush().size(suits, number)); } 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: