Artifact Content
Not logged in

Artifact 498d0330a61d76ab4ab6793823bfe304f4e1f2f7


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const LL MODVAL = 1000000007;
class AdjacentSwaps { public:
	int theCount(vector <int> p)
	{
		int N = p.size();

		vector< set<int> > child(N-1);
		for(int s=0; s<N; ++s)
		{
			int e = find(p.begin(), p.end(), s) - p.begin();
			if( s < e ) {
				// s-1 <-- s --> s+1 --> ... --> e-1 <-- e
				if(s-1>=0) child[s].insert(s-1);
				for(int i=s; i+1<=e-1; ++i) child[i].insert(i+1);
				if(e<N-1) child[e].insert(e-1);
			}
			else if( s > e ) {
				// e-1 --> e <-- e+1 <-- ... <-- s-1 --> s
				if(e-1>=0) child[e-1].insert(e);
				for(int i=e; i+1<=s-1; ++i) child[i+1].insert(i);
				if(s<N-1) child[s-1].insert(s);
			}
			else // s == e
				return 0;
		}

		// cycle
		N--;
		for(int i=0; i+1<N; ++i)
			if( child[i].count(i+1) && child[i+1].count(i) )
				return 0;

		vector<int> q(N-1);
		for(int i=0; i+1<N; ++i) {
			if( child[i].count(i+1) )
				q[i] = +1;
			else
				q[i] = -1;
		}

		LL sum = 0;
		for(int i=0; i<N; ++i)
			sum += rec(q, 0, i, N-i-1);
		return int(sum % MODVAL);
	}

	map<pair<int,int>, LL> memo;
	LL rec(const vector<int>& q, int i, int x, int y)
	{
		if( i == q.size() )
			return 1;
		pair<int,int> key(i,x);
		if( memo.count(key) )
			return memo[key];

		if( q[i] == -1 ) {
			LL sum = 0;
			for(int k=0; k<x; ++k)
				sum += rec(q, i+1, k, y+(x-k-1));
			return memo[key] = sum % MODVAL;
		} else {
			LL sum = 0;
			for(int k=0; k<y; ++k)
				sum += rec(q, i+1, x+k, y-k-1);
			return memo[key] = sum % MODVAL;
		}
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, AdjacentSwaps().theCount(p));}
int main(){

CASE(0)
	int p_[] = {1, 2, 0};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = 1; 
END
CASE(1)
	int p_[] = {0, 1};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = 0; 
END
CASE(2)
	int p_[] = {2, 0, 3, 1};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = 2; 
END
CASE(3)
	int p_[] = {1, 0, 3, 2};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = 0; 
END
CASE(4)
	int p_[] = {1, 3, 0, 5, 2, 7, 4, 8, 10, 6, 12, 9, 14, 11, 16, 13, 18, 15, 19, 17};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = 716743312; 
END
/*
CASE(5)
	int p_[] = ;
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = ; 
END
CASE(6)
	int p_[] = ;
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int _ = ; 
END
*/
}
// END CUT HERE