Artifact Content
Not logged in

Artifact a8ce90d2d41ecf1dff055cb1447c576f1adaf0b5


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

static const int MODVAL = 1000000007;
struct mint
{
	int val;
	mint():val(0){}
	mint(int    x):val(x%MODVAL) {}
	mint(size_t x):val(x%MODVAL) {}
	mint(LL     x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

class ColorfulCupcakesDivTwo { public:
	int countArrangements(string cupcakes)
	{
		vector<int> num(3);
		for(int i=0; i<cupcakes.size(); ++i)
			num[cupcakes[i]-'A']++;
		return solve(cupcakes.size(), num).val;
	}

	mint solve(int N, const vector<int>& num)
	{
		return (num[0] ? rec(0, 0, num[0]-1, num[1], num[2]) : 0)
		     + (num[1] ? rec(1, 1, num[0], num[1]-1, num[2]) : 0)
		     + (num[2] ? rec(2, 2, num[0], num[1], num[2]-1) : 0);
	}

	map<int, mint> memo;
	mint rec(int F, int P, int A, int B, int C)
	{
		if( (A+B+C) == 1 )
		{
			if(A && F!=0 && P!=0 || B && F!=1 && P!=1 || C && F!=2 && P!=2)
				return 1;
			else
				return 0;
		}

		int key = ((((F*3+P)*50+A)*50)+B)*50+C;
		if( memo.count(key) )
			return memo[key];

		mint sum = 0;
		if(A && P!=0) sum += rec(F, 0, A-1, B, C);
		if(B && P!=1) sum += rec(F, 1, A, B-1, C);
		if(C && P!=2) sum += rec(F, 2, A, B, C-1);
		return memo[key] = sum;
	}
};

// 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(_, ColorfulCupcakesDivTwo().countArrangements(cupcakes));}
int main(){

CASE(0)
	string cupcakes = "ABAB"; 
	int _ = 2; 
END
CASE(1)
	string cupcakes = "ABABA"; 
	int _ = 0; 
END
CASE(2)
	string cupcakes = "ABC"; 
	int _ = 6; 
END
CASE(3)
	string cupcakes = "ABABABABABABABABABABABABABABABABABABABABABABABABAB"; 
	int _ = 2; 
END
CASE(4)
	string cupcakes = "BCBABBACBABABCCCCCAABBAACBBBBCBCAAA"; 
	int _ = 741380640; 
END
/*
CASE(5)
	string cupcakes = ; 
	int _ = ; 
END
CASE(6)
	string cupcakes = ; 
	int _ = ; 
END
*/
}
// END CUT HERE