9d33e96a5e 2012-03-31 kinaba: #include <iostream> 9d33e96a5e 2012-03-31 kinaba: #include <sstream> 9d33e96a5e 2012-03-31 kinaba: #include <iomanip> 9d33e96a5e 2012-03-31 kinaba: #include <vector> 9d33e96a5e 2012-03-31 kinaba: #include <string> 9d33e96a5e 2012-03-31 kinaba: #include <map> 9d33e96a5e 2012-03-31 kinaba: #include <set> 9d33e96a5e 2012-03-31 kinaba: #include <algorithm> 9d33e96a5e 2012-03-31 kinaba: #include <numeric> 9d33e96a5e 2012-03-31 kinaba: #include <iterator> 9d33e96a5e 2012-03-31 kinaba: #include <functional> 9d33e96a5e 2012-03-31 kinaba: #include <complex> 9d33e96a5e 2012-03-31 kinaba: #include <queue> 9d33e96a5e 2012-03-31 kinaba: #include <stack> 9d33e96a5e 2012-03-31 kinaba: #include <cmath> 9d33e96a5e 2012-03-31 kinaba: #include <cassert> 9d33e96a5e 2012-03-31 kinaba: #include <cstring> 9d33e96a5e 2012-03-31 kinaba: #ifdef __GNUC__ 9d33e96a5e 2012-03-31 kinaba: #include <ext/hash_map> 9d33e96a5e 2012-03-31 kinaba: #define unordered_map __gnu_cxx::hash_map 9d33e96a5e 2012-03-31 kinaba: #else 9d33e96a5e 2012-03-31 kinaba: #include <unordered_map> 9d33e96a5e 2012-03-31 kinaba: #endif 9d33e96a5e 2012-03-31 kinaba: using namespace std; 9d33e96a5e 2012-03-31 kinaba: typedef long long LL; 9d33e96a5e 2012-03-31 kinaba: typedef complex<double> CMP; 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: class KingXMagicSpells { public: 9d33e96a5e 2012-03-31 kinaba: double expectedNumber(vector <int> ducks, vector <int> spellOne, vector <int> spellTwo, int K) 9d33e96a5e 2012-03-31 kinaba: { 9d33e96a5e 2012-03-31 kinaba: const int N = ducks.size(); 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: vector<int> revSpellTwo(N); 9d33e96a5e 2012-03-31 kinaba: for(int i=0; i<N; ++i) revSpellTwo[spellTwo[i]] = i; 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: double e = 0; 9d33e96a5e 2012-03-31 kinaba: for(int B=30; B>=0; --B) { 9d33e96a5e 2012-03-31 kinaba: vector<int> dd(N); 9d33e96a5e 2012-03-31 kinaba: for(int i=0; i<N; ++i) 9d33e96a5e 2012-03-31 kinaba: dd[i] = (ducks[i] & (1<<B) ? 1 : 0); 9d33e96a5e 2012-03-31 kinaba: vector<int> xx(N); 9d33e96a5e 2012-03-31 kinaba: for(int i=0; i<N; ++i) 9d33e96a5e 2012-03-31 kinaba: xx[i] = (spellOne[i] & (1<<B) ? 1 : 0); 9d33e96a5e 2012-03-31 kinaba: e += solve(dd, xx, revSpellTwo, K) * (1<<B); 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: return e; 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: double solve(const vector <int>& dd, const vector <int>& xx, const vector <int>& moveBack, int K) 9d33e96a5e 2012-03-31 kinaba: { 9d33e96a5e 2012-03-31 kinaba: memo.clear(); 9d33e96a5e 2012-03-31 kinaba: return prob(dd, xx, moveBack, K, 0, 1); 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: map<int, double> memo; 9d33e96a5e 2012-03-31 kinaba: double prob( 9d33e96a5e 2012-03-31 kinaba: const vector <int>& dd, const vector <int>& xx, const vector <int>& moveBack, 9d33e96a5e 2012-03-31 kinaba: int K, int room, int bit) { 9d33e96a5e 2012-03-31 kinaba: if( K == 0 ) { 9d33e96a5e 2012-03-31 kinaba: return dd[room]==bit ? 1.0 : 0.0; 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: int key = (K * 100 + room) * 2 + bit; 9d33e96a5e 2012-03-31 kinaba: if( memo.count(key) ) 9d33e96a5e 2012-03-31 kinaba: return memo[key]; 9d33e96a5e 2012-03-31 kinaba: double e = 0.5 * prob(dd,xx,moveBack,K-1,moveBack[room],bit) 9d33e96a5e 2012-03-31 kinaba: + 0.5 * prob(dd,xx,moveBack,K-1,room,bit^xx[room]); 9d33e96a5e 2012-03-31 kinaba: return memo[key] = e; 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: }; 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: // BEGIN CUT HERE 9d33e96a5e 2012-03-31 kinaba: #include <ctime> 9d33e96a5e 2012-03-31 kinaba: double start_time; string timer() 9d33e96a5e 2012-03-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9d33e96a5e 2012-03-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9d33e96a5e 2012-03-31 kinaba: { os << "{ "; 9d33e96a5e 2012-03-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9d33e96a5e 2012-03-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9d33e96a5e 2012-03-31 kinaba: void verify_case(const double& Expected, const double& Received) { 9d33e96a5e 2012-03-31 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 9d33e96a5e 2012-03-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9d33e96a5e 2012-03-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9d33e96a5e 2012-03-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9d33e96a5e 2012-03-31 kinaba: #define END verify_case(_, KingXMagicSpells().expectedNumber(ducks, spellOne, spellTwo, K));} 9d33e96a5e 2012-03-31 kinaba: int main(){ 9d33e96a5e 2012-03-31 kinaba: 9d33e96a5e 2012-03-31 kinaba: CASE(0) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = {5, 3, 7}; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = {1, 7, 4}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = {1, 0, 2}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = 1; 9d33e96a5e 2012-03-31 kinaba: double _ = 3.5; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(1) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = {5, 8}; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = {53, 7}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = {1, 0}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = 2; 9d33e96a5e 2012-03-31 kinaba: double _ = 21.5; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(2) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = {123, 456, 789, 1234, 56789}; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = {0, 0, 0, 0, 0}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = {0, 1, 2, 3, 4}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = 50; 9d33e96a5e 2012-03-31 kinaba: double _ = 123.0; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(3) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = {83291232, 3124231, 192412, 3813249, 629231, 9998989}; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = {58, 37, 44, 66, 72, 19}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = {2, 0, 1, 5, 4, 3}; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = 11; 9d33e96a5e 2012-03-31 kinaba: double _ = 2.888186784716797E7; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: /* 9d33e96a5e 2012-03-31 kinaba: CASE(4) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = ; 9d33e96a5e 2012-03-31 kinaba: double _ = ; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: CASE(5) 9d33e96a5e 2012-03-31 kinaba: int ducks_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 9d33e96a5e 2012-03-31 kinaba: int spellOne_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 9d33e96a5e 2012-03-31 kinaba: int spellTwo_[] = ; 9d33e96a5e 2012-03-31 kinaba: vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 9d33e96a5e 2012-03-31 kinaba: int K = ; 9d33e96a5e 2012-03-31 kinaba: double _ = ; 9d33e96a5e 2012-03-31 kinaba: END 9d33e96a5e 2012-03-31 kinaba: */ 9d33e96a5e 2012-03-31 kinaba: } 9d33e96a5e 2012-03-31 kinaba: // END CUT HERE