Artifact Content
Not logged in

Artifact 814caf84cee0379a09236340622564383e7f1c05


#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 KingXMagicSpells { public:
	double expectedNumber(vector <int> ducks, vector <int> spellOne, vector <int> spellTwo, int K)
	{
		const int N = ducks.size();

		vector<int> revSpellTwo(N);
		for(int i=0; i<N; ++i) revSpellTwo[spellTwo[i]] = i;

		double e = 0;
		for(int B=30; B>=0; --B) {
			vector<int> dd(N);
			for(int i=0; i<N; ++i)
				dd[i] = (ducks[i] & (1<<B) ? 1 : 0);
			vector<int> xx(N);
			for(int i=0; i<N; ++i)
				xx[i] = (spellOne[i] & (1<<B) ? 1 : 0);
			e += solve(dd, xx, revSpellTwo, K) * (1<<B);
		}
		return e;
	}

	double solve(const vector <int>& dd, const vector <int>& xx, const vector <int>& moveBack, int K)
	{
		memo.clear();
		return prob(dd, xx, moveBack, K, 0, 1);
	}

	map<int, double> memo;
	double prob(
		const vector <int>& dd, const vector <int>& xx, const vector <int>& moveBack,
		int K, int room, int bit) {
		if( K == 0 ) {
			return dd[room]==bit ? 1.0 : 0.0;
		}

		int key = (K * 100 + room) * 2 + bit;
		if( memo.count(key) )
			return memo[key];
		double e = 0.5 * prob(dd,xx,moveBack,K-1,moveBack[room],bit)
		         + 0.5 * prob(dd,xx,moveBack,K-1,room,bit^xx[room]);
		return memo[key] = e;
	}
};

// 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(_, KingXMagicSpells().expectedNumber(ducks, spellOne, spellTwo, K));}
int main(){

CASE(0)
	int ducks_[] = {5, 3, 7};
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = {1, 7, 4};
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = {1, 0, 2};
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = 1; 
	double _ = 3.5; 
END
CASE(1)
	int ducks_[] = {5, 8};
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = {53, 7};
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = {1, 0};
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = 2; 
	double _ = 21.5; 
END
CASE(2)
	int ducks_[] = {123, 456, 789, 1234, 56789};
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = {0, 0, 0, 0, 0};
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = {0, 1, 2, 3, 4};
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = 50; 
	double _ = 123.0; 
END
CASE(3)
	int ducks_[] = {83291232, 3124231, 192412, 3813249, 629231, 9998989};
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = {58, 37, 44, 66, 72, 19};
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = {2, 0, 1, 5, 4, 3};
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = 11; 
	double _ = 2.888186784716797E7; 
END
/*
CASE(4)
	int ducks_[] = ;
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = ;
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = ;
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = ; 
	double _ = ; 
END
CASE(5)
	int ducks_[] = ;
	  vector <int> ducks(ducks_, ducks_+sizeof(ducks_)/sizeof(*ducks_)); 
	int spellOne_[] = ;
	  vector <int> spellOne(spellOne_, spellOne_+sizeof(spellOne_)/sizeof(*spellOne_)); 
	int spellTwo_[] = ;
	  vector <int> spellTwo(spellTwo_, spellTwo_+sizeof(spellTwo_)/sizeof(*spellTwo_)); 
	int K = ; 
	double _ = ; 
END
*/
}
// END CUT HERE