6bf49a0393 2012-12-20 kinaba: #include <iostream> 6bf49a0393 2012-12-20 kinaba: #include <sstream> 6bf49a0393 2012-12-20 kinaba: #include <iomanip> 6bf49a0393 2012-12-20 kinaba: #include <vector> 6bf49a0393 2012-12-20 kinaba: #include <string> 6bf49a0393 2012-12-20 kinaba: #include <map> 6bf49a0393 2012-12-20 kinaba: #include <set> 6bf49a0393 2012-12-20 kinaba: #include <algorithm> 6bf49a0393 2012-12-20 kinaba: #include <numeric> 6bf49a0393 2012-12-20 kinaba: #include <iterator> 6bf49a0393 2012-12-20 kinaba: #include <functional> 6bf49a0393 2012-12-20 kinaba: #include <complex> 6bf49a0393 2012-12-20 kinaba: #include <queue> 6bf49a0393 2012-12-20 kinaba: #include <stack> 6bf49a0393 2012-12-20 kinaba: #include <cmath> 6bf49a0393 2012-12-20 kinaba: #include <cassert> 6bf49a0393 2012-12-20 kinaba: using namespace std; 6bf49a0393 2012-12-20 kinaba: typedef long long LL; 6bf49a0393 2012-12-20 kinaba: typedef long double LD; 6bf49a0393 2012-12-20 kinaba: typedef complex<LD> CMP; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: class AlternateColors2 { public: 6bf49a0393 2012-12-20 kinaba: long long countWays(int n, int k) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: LL total = 0; 6bf49a0393 2012-12-20 kinaba: for(int mini=0; mini*3<=n; ++mini) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: if(mini*3 == n) { 6bf49a0393 2012-12-20 kinaba: total += rgb(mini, mini, mini, k) ? 1 : 0; 6bf49a0393 2012-12-20 kinaba: } else { 6bf49a0393 2012-12-20 kinaba: total += rgb(mini, mini, n-mini*2, k) ? 1 : 0; 6bf49a0393 2012-12-20 kinaba: total += rgb(mini, n-mini*2, mini, k) ? 1 : 0; 6bf49a0393 2012-12-20 kinaba: total += rgb(n-mini*2, mini, mini, k) ? 1 : 0; 6bf49a0393 2012-12-20 kinaba: int free = n - mini*3; 6bf49a0393 2012-12-20 kinaba: if(free >= 2) { 6bf49a0393 2012-12-20 kinaba: if(k <= mini*3) { 6bf49a0393 2012-12-20 kinaba: if((k-1)%3==0) { 6bf49a0393 2012-12-20 kinaba: total += (free - 1)*3; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } else { 6bf49a0393 2012-12-20 kinaba: total += twoball(free, k-mini*3) * 2; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: return total; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: bool rgb(int r, int g, int b, int k) { 6bf49a0393 2012-12-20 kinaba: int mini = min(r, min(g, b)); 6bf49a0393 2012-12-20 kinaba: if( k <= mini*3 ) 6bf49a0393 2012-12-20 kinaba: return (k-1)%3==0; 6bf49a0393 2012-12-20 kinaba: r -= mini; 6bf49a0393 2012-12-20 kinaba: g -= mini; 6bf49a0393 2012-12-20 kinaba: b -= mini; 6bf49a0393 2012-12-20 kinaba: k -= mini*3; 6bf49a0393 2012-12-20 kinaba: if(r==0) return false; 6bf49a0393 2012-12-20 kinaba: if(g==0) return xx(r,b,k); 6bf49a0393 2012-12-20 kinaba: if(b==0) return xx(r,g,k); 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: bool xx(int r, int g, int k) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: int mini = min(r,g); 6bf49a0393 2012-12-20 kinaba: if( k <= mini*2 ) 6bf49a0393 2012-12-20 kinaba: return (k-1)%2 == 0; 6bf49a0393 2012-12-20 kinaba: r -= mini; 6bf49a0393 2012-12-20 kinaba: g -= mini; 6bf49a0393 2012-12-20 kinaba: k -= mini*2; 6bf49a0393 2012-12-20 kinaba: return r>0; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: LL twoball(int n, int k) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: int MM = (k-1)/2; 6bf49a0393 2012-12-20 kinaba: LL total = MM; 6bf49a0393 2012-12-20 kinaba: if( (MM+1)*2 <= n && (k-1)%2==0 ) 6bf49a0393 2012-12-20 kinaba: total += (n-(MM+1)*2 + 1); 6bf49a0393 2012-12-20 kinaba: return total; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: }; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: // BEGIN CUT HERE 6bf49a0393 2012-12-20 kinaba: #include <ctime> 6bf49a0393 2012-12-20 kinaba: double start_time; string timer() 6bf49a0393 2012-12-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6bf49a0393 2012-12-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6bf49a0393 2012-12-20 kinaba: { os << "{ "; 6bf49a0393 2012-12-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6bf49a0393 2012-12-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6bf49a0393 2012-12-20 kinaba: void verify_case(const long long& Expected, const long long& Received) { 6bf49a0393 2012-12-20 kinaba: bool ok = (Expected == Received); 6bf49a0393 2012-12-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6bf49a0393 2012-12-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6bf49a0393 2012-12-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6bf49a0393 2012-12-20 kinaba: #define END verify_case(_, AlternateColors2().countWays(n, k));} 6bf49a0393 2012-12-20 kinaba: int main(){ 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: CASE(0) 6bf49a0393 2012-12-20 kinaba: int n = 1; 6bf49a0393 2012-12-20 kinaba: int k = 1; 6bf49a0393 2012-12-20 kinaba: long long _ = 1LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(1) 6bf49a0393 2012-12-20 kinaba: int n = 3; 6bf49a0393 2012-12-20 kinaba: int k = 3; 6bf49a0393 2012-12-20 kinaba: long long _ = 3LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(2) 6bf49a0393 2012-12-20 kinaba: int n = 6; 6bf49a0393 2012-12-20 kinaba: int k = 4; 6bf49a0393 2012-12-20 kinaba: long long _ = 9LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(3) 6bf49a0393 2012-12-20 kinaba: int n = 6; 6bf49a0393 2012-12-20 kinaba: int k = 1; 6bf49a0393 2012-12-20 kinaba: long long _ = 21LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(4) 6bf49a0393 2012-12-20 kinaba: int n = 1000; 6bf49a0393 2012-12-20 kinaba: int k = 2; 6bf49a0393 2012-12-20 kinaba: long long _ = 1LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(5) 6bf49a0393 2012-12-20 kinaba: int n = 100000; 6bf49a0393 2012-12-20 kinaba: int k = 100000; 6bf49a0393 2012-12-20 kinaba: long long _ = 1666700000LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(6) 6bf49a0393 2012-12-20 kinaba: int n = 100000; 6bf49a0393 2012-12-20 kinaba: int k = 100; 6bf49a0393 2012-12-20 kinaba: long long _ = -1LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: /* 6bf49a0393 2012-12-20 kinaba: CASE(7) 6bf49a0393 2012-12-20 kinaba: int n = ; 6bf49a0393 2012-12-20 kinaba: int k = ; 6bf49a0393 2012-12-20 kinaba: long long _ = LL; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: */ 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: // END CUT HERE