Artifact Content
Not logged in

Artifact 1b625ac1477fd5f00c4eee688cd288ac5ca16ec9


#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>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

int bitcnt(int m)
{
	int c = 0;
	for(;m;m>>=1)
		c += (m&1);
	return c;
}

class RabbitStepping { public:
	double getExpected(string field, int r) 
	{
		cerr<<field.size()<<endl;
		int ex = 0;
		int cn = 0;

		int N = field.size();
		for(int mask=1; mask<(1<<N); ++mask)
			if( bitcnt(mask) == r )
			{
				vector< pair<int,int> > rs;
				for(int i=0; i<N; ++i)
					if( mask & (1<<i) )
						rs.push_back(make_pair(-1,i));
				int left = run(field, rs, N);
				ex += left;
				cn += 1;
			}
		return double(ex) / cn;
	}

	int run(const string& field, vector< pair<int,int> >& rs, int size)
	{
		while( size > 2 && !rs.empty() )
		{
			oneStep(field, rs, size);
			int cnt[20] = {};
			for(int i=0; i<rs.size(); ++i)
				cnt[rs[i].second] ++;
			vector< pair<int,int> > rs2;
			for(int i=0; i<rs.size(); ++i)
				if( cnt[rs[i].second] == 1 )
					rs2.push_back(rs[i]);
			rs.swap(rs2);
		}
		return rs.size();
	}

	void oneStep(const string& field, vector< pair<int,int> >& rs, int& size)
	{
		for(int i=0; i<rs.size(); ++i) {
			int prev = rs[i].first;
			int& cur = rs[i].second;
			rs[i].first = cur;
			if( cur == 0 )
				cur ++;
			else if( cur >= size-2 )
				cur --;
			else if( field[cur]=='W' )
				cur --;
			else if( field[cur]=='B' )
				cur ++;
			else if( prev == -1 )
				cur --;
			else
				cur = prev;
		}
		--size;
	}
};

// 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(_, RabbitStepping().getExpected(field, r));}
int main(){

CASE(0)
	string field = "WRBRW"; 
	int r = 4; 
	double _ = 0.8; 
END
CASE(1)
	string field = "WWB"; 
	int r = 2; 
	double _ = 1.3333333333333333; 
END
CASE(2)
	string field = "WW"; 
	int r = 1; 
	double _ = 1.0; 
END
CASE(3)
	string field = "BBBBBBBBBB"; 
	int r = 4; 
	double _ = 0.9523809523809523; 
END
CASE(4)
	string field = "RRBRRWRRBRRW"; 
	int r = 8; 
	double _ = 0.9696969696969697; 
END
CASE(5)
	string field = "RRRRRRRRRRRRRRRRR"; 
	int r = 8; 
	double _ = -1; 
END
CASE(6)
	string field = "BBBBBBBBBBBBBBBBB"; 
	int r = 8; 
	double _ = -1; 
END
CASE(7)
	string field = "WWWWWWWWWWWWWWWWW"; 
	int r = 8; 
	double _ = -1; 
END
CASE(8)
	string field = "BWBWBWBWBWBWBWBWB"; 
	int r = 8; 
	double _ = -1; 
END
CASE(9)
	string field = "RR"; 
	int r = 2; 
	double _ = 2; 
END
CASE(9)
	string field = "RR"; 
	int r = 1; 
	double _ = 1; 
END
CASE(9)
	string field = "BRWWBRBWBWBWBRBWR"; 
	int r = 1; 
	double _ = 1; 
END
}
// END CUT HERE