4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TheHexagonsDivOne { public: 4fd800b3a8 2011-02-23 kinaba: long long count(int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // at least there must be 7<=2*n numbers... 4fd800b3a8 2011-02-23 kinaba: if( n <= 3 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // center hex has 2n possibility 4fd800b3a8 2011-02-23 kinaba: // choose others from 2(n-1) candidates 4fd800b3a8 2011-02-23 kinaba: // normalize by /6 to classify by the least number 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: vector<int> pat; 4fd800b3a8 2011-02-23 kinaba: count_pat( pat, n-1, ans ); 4fd800b3a8 2011-02-23 kinaba: return ans*2*n/6; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void count_pat( vector<int>& pat, LL n, LL& ans ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int i = pat.size(); 4fd800b3a8 2011-02-23 kinaba: if( i == 6 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL distinct = set<int>(pat.begin(), pat.end()).size(); 4fd800b3a8 2011-02-23 kinaba: LL tmp = 1; 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<distinct; ++d) 4fd800b3a8 2011-02-23 kinaba: tmp *= (n-d); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL dup = 6 - distinct; 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<dup; ++d) 4fd800b3a8 2011-02-23 kinaba: tmp *= 2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL nodup = 6 - 2*dup; 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<nodup; ++d) 4fd800b3a8 2011-02-23 kinaba: tmp *= 2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: ans += tmp; 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> use(7); 4fd800b3a8 2011-02-23 kinaba: int next = 1; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<pat.size(); ++k) { 4fd800b3a8 2011-02-23 kinaba: use[pat[k]]++; 4fd800b3a8 2011-02-23 kinaba: next = max(next, pat[k]+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( next <= n ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: pat.push_back(next); 4fd800b3a8 2011-02-23 kinaba: count_pat( pat, n, ans ); 4fd800b3a8 2011-02-23 kinaba: pat.pop_back(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int p=1; p<next; ++p) 4fd800b3a8 2011-02-23 kinaba: if( use[p]<=1 ) 4fd800b3a8 2011-02-23 kinaba: if( i==0 || (pat[i-1]!=p && (i<5 || pat[0]!=p)) ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: pat.push_back(p); 4fd800b3a8 2011-02-23 kinaba: count_pat( pat, n, ans ); 4fd800b3a8 2011-02-23 kinaba: pat.pop_back(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, TheHexagonsDivOne().count(n));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int n = 3; 4fd800b3a8 2011-02-23 kinaba: long long _ = 0LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int n = 4; 4fd800b3a8 2011-02-23 kinaba: long long _ = 256LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int n = 8; 4fd800b3a8 2011-02-23 kinaba: long long _ = 3458560LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int n = 20; 4fd800b3a8 2011-02-23 kinaba: long long _ = 11193888000LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int n = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 0LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int n = 150; 4fd800b3a8 2011-02-23 kinaba: long long _ = -1LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE