Artifact Content
Not logged in

Artifact 7c3fa8bfe90815c9dea09ee07ef9d513fb6040ec


#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 gcd(int x, int y)
{
	while(x)
		swap(x, y%=x);
	return y;
}

int lcm(int x, int y)
{
	return x/gcd(x,y)*y;
}

class ArtShift { public:
	int maxShifts(string sequence) 
	{
		int B=0, W=0;
		for(int i=0; i<sequence.size(); ++i)
			(sequence[i]=='B' ? B : W)++;
		memo.assign(31*31, set<int>());
		for(int b=0; b<=B; ++b) memo[b*31+0].insert(1);
		for(int w=0; w<=W; ++w) memo[0*31+w].insert(1);
		return *rec(B,W).rbegin();
	}

	vector< set<int> > memo;
	const set<int>& rec(int B, int W)
	{
		set<int>& result = memo[B*31+W];
		if( !result.empty() )
			return result;

		result.insert(B+W);
		for(int b= 0; b<=B; ++b)
		for(int w=!b; w<=W; ++w) {
			const set<int>& x = rec(b,w);
			const set<int>& y = rec(B-b,W-w);
			for(set<int>::const_iterator it=x.begin(); it!=x.end(); ++it)
			for(set<int>::const_iterator jt=y.begin(); jt!=y.end(); ++jt)
				result.insert(lcm(*it,*jt));
		}
		return result;
	}
};

// 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(_, ArtShift().maxShifts(sequence));}
int main(){

CASE(0)
	string sequence = "BW"; 
	int _ = 2; 
END
CASE(1)
	string sequence = "BBBWBBB"; 
	int _ = 7; 
END
CASE(2)
	string sequence = "BWBWB"; 
	int _ = 6; 
END
CASE(3)
	string sequence = "WWWWWWWWW"; 
	int _ = 1; 
END
CASE(4)
	string sequence = "WWWWWWWWWBBWB"; 
	int _ = 60; 
END
CASE(5)
	string sequence = "W"; 
	int _ = 1; 
END
CASE(6)
	string sequence = "B"; 
	int _ = 1; 
END
CASE(7)
	string sequence = "BB"; 
	int _ = 1; 
END
CASE(8)
	string sequence = "BWBWBWBWBWBWBWBWBWBWBWBWBWBWBW"; 
	int _ = -1; 
END

}
// END CUT HERE