Artifact Content
Not logged in

Artifact b54e51e3de8ee5c7eb4da06ed32b432116f49774


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

const int ANY = 0;

static const unsigned MODVAL = 1000000009;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
	mint(LL       x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

class SimilarSequences { public:
	int count(vector <int> seq, int bound)
	{
		vector< vector<int> > cand;
		cand.push_back(seq);
		for(int i=0; i<seq.size(); ++i)
		{
			vector<int> s = seq;
			s.erase(s.begin() + i);
			s.push_back(ANY);
			cand.push_back(s);
			for(int w=s.size()-1; w>0; --w) {
				swap(s[w], s[w-1]);
				cand.push_back(s);
			}
		}
		vector<int> idx;
		for(int i=0; i<cand.size(); ++i)
			idx.push_back(i);
		return rec(0, cand, idx, bound).val;
	}

	mint rec(int I, const vector<vector<int> >& cand, const vector<int>& idx, int B)
	{
		if(idx.empty())
			return 0;
		if(I == cand[0].size())
			return 1;

		mint total = 0;

		set<int> head;
		for(int k=0; k<idx.size(); ++k)
			if( cand[idx[k]][I] != ANY )
				head.insert(cand[idx[k]][I]);
		// Use an existing value.
		for(set<int>::iterator it=head.begin(); it!=head.end(); ++it)
		{
			int h = *it;
			vector<int> idx2;
			for(int k=0; k<idx.size(); ++k)
				if(cand[idx[k]][I]==ANY || cand[idx[k]][I]==h)
					idx2.push_back(idx[k]);
			total += rec(I+1, cand, idx2, B);
		}

		// Use nonexisting.
		if(B > head.size()) {
			vector<int> idx2;
			for(int k=0; k<idx.size(); ++k)
				if(cand[idx[k]][I]==ANY)
					idx2.push_back(idx[k]);
			total += rec(I+1, cand, idx2, B) * (B - head.size());
		}

		return total;
	}
};

// 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(_, SimilarSequences().count(seq, bound));}
int main(){

CASE(0)
	int seq_[] = {1, 1};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = 3; 
	int _ = 5; 
END
CASE(1)
	int seq_[] = {1, 2};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = 2; 
	int _ = 4; 
END
CASE(2)
	int seq_[] = {999999999};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = 1000000000; 
	int _ = 1000000000; 
END
CASE(3)
	int seq_[] = {1, 2, 3, 4, 5};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = 5; 
	int _ = 97; 
END
CASE(4)
	int seq_[] = {5, 8, 11, 12, 4, 1, 7, 9};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = 1000000000; 
	int _ = 999999363; 
END
/*
CASE(5)
	int seq_[] = ;
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = ; 
	int _ = ; 
END
CASE(6)
	int seq_[] = ;
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int bound = ; 
	int _ = ; 
END
*/
}
// END CUT HERE