3d2bcff745 2011-12-29 kinaba: #include <iostream> 3d2bcff745 2011-12-29 kinaba: #include <sstream> 3d2bcff745 2011-12-29 kinaba: #include <iomanip> 3d2bcff745 2011-12-29 kinaba: #include <vector> 3d2bcff745 2011-12-29 kinaba: #include <string> 3d2bcff745 2011-12-29 kinaba: #include <map> 3d2bcff745 2011-12-29 kinaba: #include <set> 3d2bcff745 2011-12-29 kinaba: #include <algorithm> 3d2bcff745 2011-12-29 kinaba: #include <numeric> 3d2bcff745 2011-12-29 kinaba: #include <iterator> 3d2bcff745 2011-12-29 kinaba: #include <functional> 3d2bcff745 2011-12-29 kinaba: #include <complex> 3d2bcff745 2011-12-29 kinaba: #include <queue> 3d2bcff745 2011-12-29 kinaba: #include <stack> 3d2bcff745 2011-12-29 kinaba: #include <cmath> 3d2bcff745 2011-12-29 kinaba: #include <cassert> 3d2bcff745 2011-12-29 kinaba: #include <cstring> 3d2bcff745 2011-12-29 kinaba: #ifdef __GNUC__ 3d2bcff745 2011-12-29 kinaba: #include <ext/hash_map> 3d2bcff745 2011-12-29 kinaba: #define unordered_map __gnu_cxx::hash_map 3d2bcff745 2011-12-29 kinaba: #else 3d2bcff745 2011-12-29 kinaba: #include <unordered_map> 3d2bcff745 2011-12-29 kinaba: #endif 3d2bcff745 2011-12-29 kinaba: using namespace std; 3d2bcff745 2011-12-29 kinaba: typedef long long LL; 3d2bcff745 2011-12-29 kinaba: typedef complex<double> CMP; 3d2bcff745 2011-12-29 kinaba: 3d2bcff745 2011-12-29 kinaba: template<typename T> 3d2bcff745 2011-12-29 kinaba: struct DP2x 3d2bcff745 2011-12-29 kinaba: { 3d2bcff745 2011-12-29 kinaba: const int N1, N2; 3d2bcff745 2011-12-29 kinaba: vector<T> data; 3d2bcff745 2011-12-29 kinaba: DP2x(int, int N2, const T& t = T()) 3d2bcff745 2011-12-29 kinaba: : N1(2), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } 3d2bcff745 2011-12-29 kinaba: T& operator()(int i1, int i2) 3d2bcff745 2011-12-29 kinaba: { i1&=1; return data[ (i1*N2)+i2 ]; } 3d2bcff745 2011-12-29 kinaba: void swap(DP2x& rhs) 3d2bcff745 2011-12-29 kinaba: { data.swap(rhs.data); } 3d2bcff745 2011-12-29 kinaba: }; 3d2bcff745 2011-12-29 kinaba: 3d2bcff745 2011-12-29 kinaba: class SPartition { public: 3d2bcff745 2011-12-29 kinaba: long long getCount(string s) 3d2bcff745 2011-12-29 kinaba: { 3d2bcff745 2011-12-29 kinaba: if( s.size()%2 != 0 ) 3d2bcff745 2011-12-29 kinaba: return 0; 3d2bcff745 2011-12-29 kinaba: 3d2bcff745 2011-12-29 kinaba: int N = s.size()/2; 3d2bcff745 2011-12-29 kinaba: DP2x<LL> dp(s.size()+1, 1<<N); 3d2bcff745 2011-12-29 kinaba: for(int i=s.size(); i>=0; --i) 3d2bcff745 2011-12-29 kinaba: for(int bits=0,ni=min(N,i); bits<(1<<ni); ++bits) 3d2bcff745 2011-12-29 kinaba: { 3d2bcff745 2011-12-29 kinaba: if( i == s.size() ) 3d2bcff745 2011-12-29 kinaba: dp(i,bits) = (bits==0 ? 1 : 0); 3d2bcff745 2011-12-29 kinaba: else { 3d2bcff745 2011-12-29 kinaba: if( bits == 0 ) { 3d2bcff745 2011-12-29 kinaba: dp(i, bits) = dp(i+1, 1) * 2; 3d2bcff745 2011-12-29 kinaba: } else { 3d2bcff745 2011-12-29 kinaba: int iMax = 0; 3d2bcff745 2011-12-29 kinaba: while( (2<<iMax) <= bits ) 3d2bcff745 2011-12-29 kinaba: ++iMax; 3d2bcff745 2011-12-29 kinaba: int oldb = s[i-1-iMax]; 3d2bcff745 2011-12-29 kinaba: int newb = s[i]; 3d2bcff745 2011-12-29 kinaba: LL tot = 0; 3d2bcff745 2011-12-29 kinaba: if( oldb == newb ) 3d2bcff745 2011-12-29 kinaba: tot += dp(i+1, (bits &~ (1<<iMax))<<1); 3d2bcff745 2011-12-29 kinaba: if( (bits<<1 | 1) < (1<<N) ) 3d2bcff745 2011-12-29 kinaba: tot += dp(i+1, bits<<1 | 1); 3d2bcff745 2011-12-29 kinaba: dp(i,bits) = tot; 3d2bcff745 2011-12-29 kinaba: } 3d2bcff745 2011-12-29 kinaba: } 3d2bcff745 2011-12-29 kinaba: } 3d2bcff745 2011-12-29 kinaba: return dp(0, 0); 3d2bcff745 2011-12-29 kinaba: } 3d2bcff745 2011-12-29 kinaba: }; 3d2bcff745 2011-12-29 kinaba: 3d2bcff745 2011-12-29 kinaba: // BEGIN CUT HERE 3d2bcff745 2011-12-29 kinaba: #include <ctime> 3d2bcff745 2011-12-29 kinaba: double start_time; string timer() 3d2bcff745 2011-12-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3d2bcff745 2011-12-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3d2bcff745 2011-12-29 kinaba: { os << "{ "; 3d2bcff745 2011-12-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3d2bcff745 2011-12-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3d2bcff745 2011-12-29 kinaba: void verify_case(const long long& Expected, const long long& Received) { 3d2bcff745 2011-12-29 kinaba: bool ok = (Expected == Received); 3d2bcff745 2011-12-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3d2bcff745 2011-12-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3d2bcff745 2011-12-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3d2bcff745 2011-12-29 kinaba: #define END verify_case(_, SPartition().getCount(s));} 3d2bcff745 2011-12-29 kinaba: int main(){ 3d2bcff745 2011-12-29 kinaba: 3d2bcff745 2011-12-29 kinaba: CASE(0) 3d2bcff745 2011-12-29 kinaba: string s = "oxox"; 3d2bcff745 2011-12-29 kinaba: long long _ = 2LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(1) 3d2bcff745 2011-12-29 kinaba: string s = "oooxxx"; 3d2bcff745 2011-12-29 kinaba: long long _ = 0LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(2) 3d2bcff745 2011-12-29 kinaba: string s = "xoxxox"; 3d2bcff745 2011-12-29 kinaba: long long _ = 4LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(3) 3d2bcff745 2011-12-29 kinaba: string s = "xo"; 3d2bcff745 2011-12-29 kinaba: long long _ = 0LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(4) 3d2bcff745 2011-12-29 kinaba: string s = "ooooxoox"; 3d2bcff745 2011-12-29 kinaba: long long _ = 8LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(5) 3d2bcff745 2011-12-29 kinaba: string s = "ooxxoxox"; 3d2bcff745 2011-12-29 kinaba: long long _ = 8LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: CASE(6) 3d2bcff745 2011-12-29 kinaba: string s = "oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox"; 3d2bcff745 2011-12-29 kinaba: long long _ = -1LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: /* 3d2bcff745 2011-12-29 kinaba: CASE(7) 3d2bcff745 2011-12-29 kinaba: string s = ; 3d2bcff745 2011-12-29 kinaba: long long _ = LL; 3d2bcff745 2011-12-29 kinaba: END 3d2bcff745 2011-12-29 kinaba: */ 3d2bcff745 2011-12-29 kinaba: } 3d2bcff745 2011-12-29 kinaba: // END CUT HERE