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