4f3ed14599 2013-03-30 kinaba: #include <iostream> 4f3ed14599 2013-03-30 kinaba: #include <sstream> 4f3ed14599 2013-03-30 kinaba: #include <iomanip> 4f3ed14599 2013-03-30 kinaba: #include <vector> 4f3ed14599 2013-03-30 kinaba: #include <string> 4f3ed14599 2013-03-30 kinaba: #include <map> 4f3ed14599 2013-03-30 kinaba: #include <set> 4f3ed14599 2013-03-30 kinaba: #include <algorithm> 4f3ed14599 2013-03-30 kinaba: #include <numeric> 4f3ed14599 2013-03-30 kinaba: #include <iterator> 4f3ed14599 2013-03-30 kinaba: #include <functional> 4f3ed14599 2013-03-30 kinaba: #include <complex> 4f3ed14599 2013-03-30 kinaba: #include <queue> 4f3ed14599 2013-03-30 kinaba: #include <stack> 4f3ed14599 2013-03-30 kinaba: #include <cmath> 4f3ed14599 2013-03-30 kinaba: #include <cassert> 4f3ed14599 2013-03-30 kinaba: using namespace std; 4f3ed14599 2013-03-30 kinaba: typedef long long LL; 4f3ed14599 2013-03-30 kinaba: typedef long double LD; 4f3ed14599 2013-03-30 kinaba: typedef complex<LD> CMP; 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: class PolygonTraversal { public: 4f3ed14599 2013-03-30 kinaba: long long count(int N, vector <int> points) 4f3ed14599 2013-03-30 kinaba: { 4f3ed14599 2013-03-30 kinaba: int p0 = points[0]; 4f3ed14599 2013-03-30 kinaba: for(int i=0; i<points.size(); ++i) 4f3ed14599 2013-03-30 kinaba: points[i] = (points[i]-p0+N)%N; 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: int mask = 0; 4f3ed14599 2013-03-30 kinaba: for(int i=0; i<points.size(); ++i) 4f3ed14599 2013-03-30 kinaba: mask |= (1 << points[i]); 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: vector<LL> memo(N<<N, -1); 4f3ed14599 2013-03-30 kinaba: return rec(mask, points.back(), N, memo); 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: LL rec(int mask, int i, int N, vector<LL>& memo) 4f3ed14599 2013-03-30 kinaba: { 4f3ed14599 2013-03-30 kinaba: if(mask == (1<<N)-1) { 4f3ed14599 2013-03-30 kinaba: return (i==1 || i==N-1) ? 0 : 1; 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: int key = (i<<N) | mask; 4f3ed14599 2013-03-30 kinaba: if( memo[key] != -1 ) 4f3ed14599 2013-03-30 kinaba: return memo[key]; 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: LL total = 0; 4f3ed14599 2013-03-30 kinaba: for(int k=0; k<N; ++k) if(!(mask&(1<<k))) { 4f3ed14599 2013-03-30 kinaba: int base = min(i, k); 4f3ed14599 2013-03-30 kinaba: int mid = max(i,k)-base; 4f3ed14599 2013-03-30 kinaba: int left=0, right=0; 4f3ed14599 2013-03-30 kinaba: for(int j=0; j<N; ++j) if(mask&(1<<j)) { 4f3ed14599 2013-03-30 kinaba: int idx = (j-base+N)%N; 4f3ed14599 2013-03-30 kinaba: if(0<idx&&idx<mid)++left; 4f3ed14599 2013-03-30 kinaba: if(mid<idx&&idx<N)++right; 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: if(left&&right) 4f3ed14599 2013-03-30 kinaba: total += rec(mask|(1<<k), k, N, memo); 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: return memo[key] = total; 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: }; 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: // BEGIN CUT HERE 4f3ed14599 2013-03-30 kinaba: #include <ctime> 4f3ed14599 2013-03-30 kinaba: double start_time; string timer() 4f3ed14599 2013-03-30 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4f3ed14599 2013-03-30 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4f3ed14599 2013-03-30 kinaba: { os << "{ "; 4f3ed14599 2013-03-30 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4f3ed14599 2013-03-30 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4f3ed14599 2013-03-30 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4f3ed14599 2013-03-30 kinaba: bool ok = (Expected == Received); 4f3ed14599 2013-03-30 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4f3ed14599 2013-03-30 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4f3ed14599 2013-03-30 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4f3ed14599 2013-03-30 kinaba: #define END verify_case(_, PolygonTraversal().count(N, points));} 4f3ed14599 2013-03-30 kinaba: int main(){ 4f3ed14599 2013-03-30 kinaba: 4f3ed14599 2013-03-30 kinaba: CASE(0) 4f3ed14599 2013-03-30 kinaba: int N = 5; 4f3ed14599 2013-03-30 kinaba: int points_[] = {1, 3, 5}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = 1LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: CASE(1) 4f3ed14599 2013-03-30 kinaba: int N = 6; 4f3ed14599 2013-03-30 kinaba: int points_[] = {1, 4, 2}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = 1LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: CASE(2) 4f3ed14599 2013-03-30 kinaba: int N = 7; 4f3ed14599 2013-03-30 kinaba: int points_[] = {2, 4, 7}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = 2LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: CASE(3) 4f3ed14599 2013-03-30 kinaba: int N = 7; 4f3ed14599 2013-03-30 kinaba: int points_[] = {1, 2, 3, 4, 6, 5}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = 0LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: CASE(4) 4f3ed14599 2013-03-30 kinaba: int N = 18; 4f3ed14599 2013-03-30 kinaba: int points_[] = {1, 7, 18}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = 4374612736LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: CASE(5) 4f3ed14599 2013-03-30 kinaba: int N = 18; 4f3ed14599 2013-03-30 kinaba: int points_[] = {1,8}; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = -2LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: /* 4f3ed14599 2013-03-30 kinaba: CASE(6) 4f3ed14599 2013-03-30 kinaba: int N = ; 4f3ed14599 2013-03-30 kinaba: int points_[] = ; 4f3ed14599 2013-03-30 kinaba: vector <int> points(points_, points_+sizeof(points_)/sizeof(*points_)); 4f3ed14599 2013-03-30 kinaba: long long _ = LL; 4f3ed14599 2013-03-30 kinaba: END 4f3ed14599 2013-03-30 kinaba: */ 4f3ed14599 2013-03-30 kinaba: } 4f3ed14599 2013-03-30 kinaba: // END CUT HERE