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