99eae4eb4a 2011-12-28 kinaba: #include <iostream> 99eae4eb4a 2011-12-28 kinaba: #include <sstream> 99eae4eb4a 2011-12-28 kinaba: #include <iomanip> 99eae4eb4a 2011-12-28 kinaba: #include <vector> 99eae4eb4a 2011-12-28 kinaba: #include <string> 99eae4eb4a 2011-12-28 kinaba: #include <map> 99eae4eb4a 2011-12-28 kinaba: #include <set> 99eae4eb4a 2011-12-28 kinaba: #include <algorithm> 99eae4eb4a 2011-12-28 kinaba: #include <numeric> 99eae4eb4a 2011-12-28 kinaba: #include <iterator> 99eae4eb4a 2011-12-28 kinaba: #include <functional> 99eae4eb4a 2011-12-28 kinaba: #include <complex> 99eae4eb4a 2011-12-28 kinaba: #include <queue> 99eae4eb4a 2011-12-28 kinaba: #include <stack> 99eae4eb4a 2011-12-28 kinaba: #include <cmath> 99eae4eb4a 2011-12-28 kinaba: #include <cassert> 99eae4eb4a 2011-12-28 kinaba: #include <cstring> 99eae4eb4a 2011-12-28 kinaba: #ifdef __GNUC__ 99eae4eb4a 2011-12-28 kinaba: #include <ext/hash_map> 99eae4eb4a 2011-12-28 kinaba: #define unordered_map __gnu_cxx::hash_map 99eae4eb4a 2011-12-28 kinaba: #else 99eae4eb4a 2011-12-28 kinaba: #include <unordered_map> 99eae4eb4a 2011-12-28 kinaba: #endif 99eae4eb4a 2011-12-28 kinaba: using namespace std; 99eae4eb4a 2011-12-28 kinaba: typedef long long LL; 99eae4eb4a 2011-12-28 kinaba: typedef complex<double> CMP; 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: class MagicBlizzard { public: 99eae4eb4a 2011-12-28 kinaba: double expectation(vector <int> range, vector <int> amount) 99eae4eb4a 2011-12-28 kinaba: { 99eae4eb4a 2011-12-28 kinaba: map<LL,int> ram; 99eae4eb4a 2011-12-28 kinaba: for(int i=0; i<range.size(); ++i) 99eae4eb4a 2011-12-28 kinaba: ram[range[i]] += amount[i]; 99eae4eb4a 2011-12-28 kinaba: vector< pair<LL,int> > ra(ram.begin(), ram.end()); 99eae4eb4a 2011-12-28 kinaba: int all = accumulate(amount.begin(), amount.end(), 0); 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: double e = 0; 99eae4eb4a 2011-12-28 kinaba: for(int i=0; i<ra.size(); ++i) 99eae4eb4a 2011-12-28 kinaba: { 99eae4eb4a 2011-12-28 kinaba: LL r = ra[i].first; 99eae4eb4a 2011-12-28 kinaba: int a = ra[i].second; 99eae4eb4a 2011-12-28 kinaba: LL r_area = (2*r+1)*(2*r+1) - (i==0 ? 0 : (2*ra[i-1].first+1)*(2*ra[i-1].first+1)); 99eae4eb4a 2011-12-28 kinaba: for(int sf=0; sf<=all; ++sf) 99eae4eb4a 2011-12-28 kinaba: e += rec(ra, i, sf, all) * r_area * sf * sf; 99eae4eb4a 2011-12-28 kinaba: all -= a; 99eae4eb4a 2011-12-28 kinaba: } 99eae4eb4a 2011-12-28 kinaba: return e; 99eae4eb4a 2011-12-28 kinaba: } 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: map<pair<int,int>, double> memo; 99eae4eb4a 2011-12-28 kinaba: double rec(vector< pair<LL,int> >& ra, int i, int snow_fall, int all) 99eae4eb4a 2011-12-28 kinaba: { 99eae4eb4a 2011-12-28 kinaba: if( i == ra.size() ) 99eae4eb4a 2011-12-28 kinaba: return snow_fall == 0 ? 1.0 : 0.0; 99eae4eb4a 2011-12-28 kinaba: if( all < snow_fall ) 99eae4eb4a 2011-12-28 kinaba: return 0.0; 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: pair<int,int> key(i, snow_fall); 99eae4eb4a 2011-12-28 kinaba: if( memo.count(key) ) 99eae4eb4a 2011-12-28 kinaba: return memo[key]; 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: LL r = ra[i].first; 99eae4eb4a 2011-12-28 kinaba: int a = ra[i].second; 99eae4eb4a 2011-12-28 kinaba: double p1 = double(1)/((2*r+1)*(2*r+1)); 99eae4eb4a 2011-12-28 kinaba: double p0 = 1 - p1; 99eae4eb4a 2011-12-28 kinaba: // (p0 #0 + p1 #1)^a 99eae4eb4a 2011-12-28 kinaba: double csum = 0; 99eae4eb4a 2011-12-28 kinaba: double Cak = 1; 99eae4eb4a 2011-12-28 kinaba: for(int k=0; k<=min(a, snow_fall); ++k) 99eae4eb4a 2011-12-28 kinaba: { 99eae4eb4a 2011-12-28 kinaba: // coord of ^k 99eae4eb4a 2011-12-28 kinaba: // C(a,k) p1^k p0^(a-k) 99eae4eb4a 2011-12-28 kinaba: double c = Cak * pow(p1,k) * pow(p0,a-k); 99eae4eb4a 2011-12-28 kinaba: csum += c * rec(ra, i+1, snow_fall-k, all-a); 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: Cak = Cak * (a-k) / (k+1); 99eae4eb4a 2011-12-28 kinaba: } 99eae4eb4a 2011-12-28 kinaba: return memo[key] = csum; 99eae4eb4a 2011-12-28 kinaba: } 99eae4eb4a 2011-12-28 kinaba: }; 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: // BEGIN CUT HERE 99eae4eb4a 2011-12-28 kinaba: #include <ctime> 99eae4eb4a 2011-12-28 kinaba: double start_time; string timer() 99eae4eb4a 2011-12-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 99eae4eb4a 2011-12-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 99eae4eb4a 2011-12-28 kinaba: { os << "{ "; 99eae4eb4a 2011-12-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 99eae4eb4a 2011-12-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 99eae4eb4a 2011-12-28 kinaba: void verify_case(const double& Expected, const double& Received) { 99eae4eb4a 2011-12-28 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 99eae4eb4a 2011-12-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 99eae4eb4a 2011-12-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 99eae4eb4a 2011-12-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 99eae4eb4a 2011-12-28 kinaba: #define END verify_case(_, MagicBlizzard().expectation(range, amount));} 99eae4eb4a 2011-12-28 kinaba: int main(){ 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: CASE(0) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {1,0}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {1,1}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 2.2222222222222223; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(1) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {1,0}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {2,1}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 3.666666666666667; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(2) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {5,2,6,5}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {1,2,2,3}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 8.46525111252384; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(3) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {7,11,2,13,3,19,5,17}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {16,8,4,15,12,9,10,6}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 98.55659436211914; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(4) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {0,0,0,0,0,0,0,0,0,0}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {10000,10000,10000,10000,10000,10000,10000,10000,10000,10000}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 1.0E10; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(5) 99eae4eb4a 2011-12-28 kinaba: int range_[] = {1}; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = {100}; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = 10000; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: CASE(6) 99eae4eb4a 2011-12-28 kinaba: int range_[] = { 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: }; 99eae4eb4a 2011-12-28 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 99eae4eb4a 2011-12-28 kinaba: int amount_[] = { 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: 10000,10000,10000,10000,10000,10000,10000,10000,10000,10000, 99eae4eb4a 2011-12-28 kinaba: }; 99eae4eb4a 2011-12-28 kinaba: vector <int> amount(amount_, amount_+sizeof(amount_)/sizeof(*amount_)); 99eae4eb4a 2011-12-28 kinaba: double _ = -1; 99eae4eb4a 2011-12-28 kinaba: END 99eae4eb4a 2011-12-28 kinaba: 99eae4eb4a 2011-12-28 kinaba: } 99eae4eb4a 2011-12-28 kinaba: // END CUT HERE