b98fbebb35 2011-09-14 kinaba: #include <iostream> b98fbebb35 2011-09-14 kinaba: #include <sstream> b98fbebb35 2011-09-14 kinaba: #include <iomanip> b98fbebb35 2011-09-14 kinaba: #include <vector> b98fbebb35 2011-09-14 kinaba: #include <string> b98fbebb35 2011-09-14 kinaba: #include <map> b98fbebb35 2011-09-14 kinaba: #include <set> b98fbebb35 2011-09-14 kinaba: #include <algorithm> b98fbebb35 2011-09-14 kinaba: #include <numeric> b98fbebb35 2011-09-14 kinaba: #include <iterator> b98fbebb35 2011-09-14 kinaba: #include <functional> b98fbebb35 2011-09-14 kinaba: #include <complex> b98fbebb35 2011-09-14 kinaba: #include <queue> b98fbebb35 2011-09-14 kinaba: #include <stack> b98fbebb35 2011-09-14 kinaba: #include <cmath> b98fbebb35 2011-09-14 kinaba: #include <cassert> b98fbebb35 2011-09-14 kinaba: #include <cstring> b98fbebb35 2011-09-14 kinaba: using namespace std; b98fbebb35 2011-09-14 kinaba: typedef long long LL; b98fbebb35 2011-09-14 kinaba: typedef complex<double> CMP; b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: static const LL MODVAL = 1000000007; b98fbebb35 2011-09-14 kinaba: class AdjacentSwaps { public: b98fbebb35 2011-09-14 kinaba: int theCount(vector <int> p) b98fbebb35 2011-09-14 kinaba: { b98fbebb35 2011-09-14 kinaba: int N = p.size(); b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: vector< set<int> > child(N-1); b98fbebb35 2011-09-14 kinaba: for(int s=0; s<N; ++s) b98fbebb35 2011-09-14 kinaba: { b98fbebb35 2011-09-14 kinaba: int e = find(p.begin(), p.end(), s) - p.begin(); b98fbebb35 2011-09-14 kinaba: if( s < e ) { b98fbebb35 2011-09-14 kinaba: // s-1 <-- s --> s+1 --> ... --> e-1 <-- e b98fbebb35 2011-09-14 kinaba: if(s-1>=0) child[s].insert(s-1); b98fbebb35 2011-09-14 kinaba: for(int i=s; i+1<=e-1; ++i) child[i].insert(i+1); b98fbebb35 2011-09-14 kinaba: if(e<N-1) child[e].insert(e-1); b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: else if( s > e ) { b98fbebb35 2011-09-14 kinaba: // e-1 --> e <-- e+1 <-- ... <-- s-1 --> s b98fbebb35 2011-09-14 kinaba: if(e-1>=0) child[e-1].insert(e); b98fbebb35 2011-09-14 kinaba: for(int i=e; i+1<=s-1; ++i) child[i+1].insert(i); b98fbebb35 2011-09-14 kinaba: if(s<N-1) child[s-1].insert(s); b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: else // s == e b98fbebb35 2011-09-14 kinaba: return 0; b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: // cycle b98fbebb35 2011-09-14 kinaba: N--; b98fbebb35 2011-09-14 kinaba: for(int i=0; i+1<N; ++i) b98fbebb35 2011-09-14 kinaba: if( child[i].count(i+1) && child[i+1].count(i) ) b98fbebb35 2011-09-14 kinaba: return 0; b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: vector<int> q(N-1); b98fbebb35 2011-09-14 kinaba: for(int i=0; i+1<N; ++i) { b98fbebb35 2011-09-14 kinaba: if( child[i].count(i+1) ) b98fbebb35 2011-09-14 kinaba: q[i] = +1; b98fbebb35 2011-09-14 kinaba: else b98fbebb35 2011-09-14 kinaba: q[i] = -1; b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: LL sum = 0; b98fbebb35 2011-09-14 kinaba: for(int i=0; i<N; ++i) b98fbebb35 2011-09-14 kinaba: sum += rec(q, 0, i, N-i-1); b98fbebb35 2011-09-14 kinaba: return int(sum % MODVAL); b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: map<pair<int,int>, LL> memo; b98fbebb35 2011-09-14 kinaba: LL rec(const vector<int>& q, int i, int x, int y) b98fbebb35 2011-09-14 kinaba: { b98fbebb35 2011-09-14 kinaba: if( i == q.size() ) b98fbebb35 2011-09-14 kinaba: return 1; b98fbebb35 2011-09-14 kinaba: pair<int,int> key(i,x); b98fbebb35 2011-09-14 kinaba: if( memo.count(key) ) b98fbebb35 2011-09-14 kinaba: return memo[key]; b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: if( q[i] == -1 ) { b98fbebb35 2011-09-14 kinaba: LL sum = 0; b98fbebb35 2011-09-14 kinaba: for(int k=0; k<x; ++k) b98fbebb35 2011-09-14 kinaba: sum += rec(q, i+1, k, y+(x-k-1)); b98fbebb35 2011-09-14 kinaba: return memo[key] = sum % MODVAL; b98fbebb35 2011-09-14 kinaba: } else { b98fbebb35 2011-09-14 kinaba: LL sum = 0; b98fbebb35 2011-09-14 kinaba: for(int k=0; k<y; ++k) b98fbebb35 2011-09-14 kinaba: sum += rec(q, i+1, x+k, y-k-1); b98fbebb35 2011-09-14 kinaba: return memo[key] = sum % MODVAL; b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: }; b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: // BEGIN CUT HERE b98fbebb35 2011-09-14 kinaba: #include <ctime> b98fbebb35 2011-09-14 kinaba: double start_time; string timer() b98fbebb35 2011-09-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } b98fbebb35 2011-09-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) b98fbebb35 2011-09-14 kinaba: { os << "{ "; b98fbebb35 2011-09-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) b98fbebb35 2011-09-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } b98fbebb35 2011-09-14 kinaba: void verify_case(const int& Expected, const int& Received) { b98fbebb35 2011-09-14 kinaba: bool ok = (Expected == Received); b98fbebb35 2011-09-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; b98fbebb35 2011-09-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } b98fbebb35 2011-09-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); b98fbebb35 2011-09-14 kinaba: #define END verify_case(_, AdjacentSwaps().theCount(p));} b98fbebb35 2011-09-14 kinaba: int main(){ b98fbebb35 2011-09-14 kinaba: b98fbebb35 2011-09-14 kinaba: CASE(0) b98fbebb35 2011-09-14 kinaba: int p_[] = {1, 2, 0}; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = 1; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: CASE(1) b98fbebb35 2011-09-14 kinaba: int p_[] = {0, 1}; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = 0; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: CASE(2) b98fbebb35 2011-09-14 kinaba: int p_[] = {2, 0, 3, 1}; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = 2; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: CASE(3) b98fbebb35 2011-09-14 kinaba: int p_[] = {1, 0, 3, 2}; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = 0; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: CASE(4) b98fbebb35 2011-09-14 kinaba: int p_[] = {1, 3, 0, 5, 2, 7, 4, 8, 10, 6, 12, 9, 14, 11, 16, 13, 18, 15, 19, 17}; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = 716743312; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: /* b98fbebb35 2011-09-14 kinaba: CASE(5) b98fbebb35 2011-09-14 kinaba: int p_[] = ; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = ; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: CASE(6) b98fbebb35 2011-09-14 kinaba: int p_[] = ; b98fbebb35 2011-09-14 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); b98fbebb35 2011-09-14 kinaba: int _ = ; b98fbebb35 2011-09-14 kinaba: END b98fbebb35 2011-09-14 kinaba: */ b98fbebb35 2011-09-14 kinaba: } b98fbebb35 2011-09-14 kinaba: // END CUT HERE