bc2785b91d 2012-08-17 kinaba: #include <iostream> bc2785b91d 2012-08-17 kinaba: #include <sstream> bc2785b91d 2012-08-17 kinaba: #include <iomanip> bc2785b91d 2012-08-17 kinaba: #include <vector> bc2785b91d 2012-08-17 kinaba: #include <string> bc2785b91d 2012-08-17 kinaba: #include <map> bc2785b91d 2012-08-17 kinaba: #include <set> bc2785b91d 2012-08-17 kinaba: #include <algorithm> bc2785b91d 2012-08-17 kinaba: #include <numeric> bc2785b91d 2012-08-17 kinaba: #include <iterator> bc2785b91d 2012-08-17 kinaba: #include <functional> bc2785b91d 2012-08-17 kinaba: #include <complex> bc2785b91d 2012-08-17 kinaba: #include <queue> bc2785b91d 2012-08-17 kinaba: #include <stack> bc2785b91d 2012-08-17 kinaba: #include <cmath> bc2785b91d 2012-08-17 kinaba: #include <cassert> bc2785b91d 2012-08-17 kinaba: using namespace std; bc2785b91d 2012-08-17 kinaba: typedef long long LL; bc2785b91d 2012-08-17 kinaba: typedef long double LD; bc2785b91d 2012-08-17 kinaba: typedef complex<LD> CMP; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: static const int MODVAL = 1000000007; bc2785b91d 2012-08-17 kinaba: struct mint bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int val; bc2785b91d 2012-08-17 kinaba: mint():val(0){} bc2785b91d 2012-08-17 kinaba: mint(int x):val(x%MODVAL) {} bc2785b91d 2012-08-17 kinaba: mint(size_t x):val(x%MODVAL) {} bc2785b91d 2012-08-17 kinaba: mint(LL x):val(x%MODVAL) {} bc2785b91d 2012-08-17 kinaba: }; bc2785b91d 2012-08-17 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } bc2785b91d 2012-08-17 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } bc2785b91d 2012-08-17 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } bc2785b91d 2012-08-17 kinaba: mint operator+(mint x, mint y) { return x+=y; } bc2785b91d 2012-08-17 kinaba: mint operator-(mint x, mint y) { return x-=y; } bc2785b91d 2012-08-17 kinaba: mint operator*(mint x, mint y) { return x*=y; } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: class ColorfulCupcakesDivTwo { public: bc2785b91d 2012-08-17 kinaba: int countArrangements(string cupcakes) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: vector<int> num(3); bc2785b91d 2012-08-17 kinaba: for(int i=0; i<cupcakes.size(); ++i) bc2785b91d 2012-08-17 kinaba: num[cupcakes[i]-'A']++; bc2785b91d 2012-08-17 kinaba: return solve(cupcakes.size(), num).val; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: mint solve(int N, const vector<int>& num) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: return (num[0] ? rec(0, 0, num[0]-1, num[1], num[2]) : 0) bc2785b91d 2012-08-17 kinaba: + (num[1] ? rec(1, 1, num[0], num[1]-1, num[2]) : 0) bc2785b91d 2012-08-17 kinaba: + (num[2] ? rec(2, 2, num[0], num[1], num[2]-1) : 0); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: map<int, mint> memo; bc2785b91d 2012-08-17 kinaba: mint rec(int F, int P, int A, int B, int C) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: if( (A+B+C) == 1 ) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: if(A && F!=0 && P!=0 || B && F!=1 && P!=1 || C && F!=2 && P!=2) bc2785b91d 2012-08-17 kinaba: return 1; bc2785b91d 2012-08-17 kinaba: else bc2785b91d 2012-08-17 kinaba: return 0; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: int key = ((((F*3+P)*50+A)*50)+B)*50+C; bc2785b91d 2012-08-17 kinaba: if( memo.count(key) ) bc2785b91d 2012-08-17 kinaba: return memo[key]; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: mint sum = 0; bc2785b91d 2012-08-17 kinaba: if(A && P!=0) sum += rec(F, 0, A-1, B, C); bc2785b91d 2012-08-17 kinaba: if(B && P!=1) sum += rec(F, 1, A, B-1, C); bc2785b91d 2012-08-17 kinaba: if(C && P!=2) sum += rec(F, 2, A, B, C-1); bc2785b91d 2012-08-17 kinaba: return memo[key] = sum; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: }; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // BEGIN CUT HERE bc2785b91d 2012-08-17 kinaba: #include <ctime> bc2785b91d 2012-08-17 kinaba: double start_time; string timer() bc2785b91d 2012-08-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } bc2785b91d 2012-08-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) bc2785b91d 2012-08-17 kinaba: { os << "{ "; bc2785b91d 2012-08-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) bc2785b91d 2012-08-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } bc2785b91d 2012-08-17 kinaba: void verify_case(const int& Expected, const int& Received) { bc2785b91d 2012-08-17 kinaba: bool ok = (Expected == Received); bc2785b91d 2012-08-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; bc2785b91d 2012-08-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } bc2785b91d 2012-08-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); bc2785b91d 2012-08-17 kinaba: #define END verify_case(_, ColorfulCupcakesDivTwo().countArrangements(cupcakes));} bc2785b91d 2012-08-17 kinaba: int main(){ bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: CASE(0) bc2785b91d 2012-08-17 kinaba: string cupcakes = "ABAB"; bc2785b91d 2012-08-17 kinaba: int _ = 2; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(1) bc2785b91d 2012-08-17 kinaba: string cupcakes = "ABABA"; bc2785b91d 2012-08-17 kinaba: int _ = 0; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(2) bc2785b91d 2012-08-17 kinaba: string cupcakes = "ABC"; bc2785b91d 2012-08-17 kinaba: int _ = 6; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(3) bc2785b91d 2012-08-17 kinaba: string cupcakes = "ABABABABABABABABABABABABABABABABABABABABABABABABAB"; bc2785b91d 2012-08-17 kinaba: int _ = 2; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(4) bc2785b91d 2012-08-17 kinaba: string cupcakes = "BCBABBACBABABCCCCCAABBAACBBBBCBCAAA"; bc2785b91d 2012-08-17 kinaba: int _ = 741380640; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: /* bc2785b91d 2012-08-17 kinaba: CASE(5) bc2785b91d 2012-08-17 kinaba: string cupcakes = ; bc2785b91d 2012-08-17 kinaba: int _ = ; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(6) bc2785b91d 2012-08-17 kinaba: string cupcakes = ; bc2785b91d 2012-08-17 kinaba: int _ = ; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: */ bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: // END CUT HERE