4c0c9a228b 2013-07-01 kinaba: #include <iostream> 4c0c9a228b 2013-07-01 kinaba: #include <sstream> 4c0c9a228b 2013-07-01 kinaba: #include <iomanip> 4c0c9a228b 2013-07-01 kinaba: #include <vector> 4c0c9a228b 2013-07-01 kinaba: #include <string> 4c0c9a228b 2013-07-01 kinaba: #include <map> 4c0c9a228b 2013-07-01 kinaba: #include <set> 4c0c9a228b 2013-07-01 kinaba: #include <algorithm> 4c0c9a228b 2013-07-01 kinaba: #include <numeric> 4c0c9a228b 2013-07-01 kinaba: #include <iterator> 4c0c9a228b 2013-07-01 kinaba: #include <functional> 4c0c9a228b 2013-07-01 kinaba: #include <complex> 4c0c9a228b 2013-07-01 kinaba: #include <queue> 4c0c9a228b 2013-07-01 kinaba: #include <stack> 4c0c9a228b 2013-07-01 kinaba: #include <cmath> 4c0c9a228b 2013-07-01 kinaba: #include <cassert> 4c0c9a228b 2013-07-01 kinaba: using namespace std; 4c0c9a228b 2013-07-01 kinaba: typedef long long LL; 4c0c9a228b 2013-07-01 kinaba: typedef long double LD; 4c0c9a228b 2013-07-01 kinaba: typedef complex<double> CMP; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: const int ANY = 0; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: static const unsigned MODVAL = 1000000009; 4c0c9a228b 2013-07-01 kinaba: struct mint 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: unsigned val; 4c0c9a228b 2013-07-01 kinaba: mint():val(0){} 4c0c9a228b 2013-07-01 kinaba: mint(int x):val(x%MODVAL) {} 4c0c9a228b 2013-07-01 kinaba: mint(unsigned x):val(x%MODVAL) {} 4c0c9a228b 2013-07-01 kinaba: mint(LL x):val(x%MODVAL) {} 4c0c9a228b 2013-07-01 kinaba: }; 4c0c9a228b 2013-07-01 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 4c0c9a228b 2013-07-01 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 4c0c9a228b 2013-07-01 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 4c0c9a228b 2013-07-01 kinaba: mint operator+(mint x, mint y) { return x+=y; } 4c0c9a228b 2013-07-01 kinaba: mint operator-(mint x, mint y) { return x-=y; } 4c0c9a228b 2013-07-01 kinaba: mint operator*(mint x, mint y) { return x*=y; } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: class SimilarSequences { public: 4c0c9a228b 2013-07-01 kinaba: int count(vector <int> seq, int bound) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: vector< vector<int> > cand; 4c0c9a228b 2013-07-01 kinaba: cand.push_back(seq); 4c0c9a228b 2013-07-01 kinaba: for(int i=0; i<seq.size(); ++i) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: vector<int> s = seq; 4c0c9a228b 2013-07-01 kinaba: s.erase(s.begin() + i); 4c0c9a228b 2013-07-01 kinaba: s.push_back(ANY); 4c0c9a228b 2013-07-01 kinaba: cand.push_back(s); 4c0c9a228b 2013-07-01 kinaba: for(int w=s.size()-1; w>0; --w) { 4c0c9a228b 2013-07-01 kinaba: swap(s[w], s[w-1]); 4c0c9a228b 2013-07-01 kinaba: cand.push_back(s); 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: vector<int> idx; 4c0c9a228b 2013-07-01 kinaba: for(int i=0; i<cand.size(); ++i) 4c0c9a228b 2013-07-01 kinaba: idx.push_back(i); 4c0c9a228b 2013-07-01 kinaba: return rec(0, cand, idx, bound).val; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: mint rec(int I, const vector<vector<int> >& cand, const vector<int>& idx, int B) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: if(idx.empty()) 4c0c9a228b 2013-07-01 kinaba: return 0; 4c0c9a228b 2013-07-01 kinaba: if(I == cand[0].size()) 4c0c9a228b 2013-07-01 kinaba: return 1; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: mint total = 0; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: set<int> head; 4c0c9a228b 2013-07-01 kinaba: for(int k=0; k<idx.size(); ++k) 4c0c9a228b 2013-07-01 kinaba: if( cand[idx[k]][I] != ANY ) 4c0c9a228b 2013-07-01 kinaba: head.insert(cand[idx[k]][I]); 4c0c9a228b 2013-07-01 kinaba: // Use an existing value. 4c0c9a228b 2013-07-01 kinaba: for(set<int>::iterator it=head.begin(); it!=head.end(); ++it) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: int h = *it; 4c0c9a228b 2013-07-01 kinaba: vector<int> idx2; 4c0c9a228b 2013-07-01 kinaba: for(int k=0; k<idx.size(); ++k) 4c0c9a228b 2013-07-01 kinaba: if(cand[idx[k]][I]==ANY || cand[idx[k]][I]==h) 4c0c9a228b 2013-07-01 kinaba: idx2.push_back(idx[k]); 4c0c9a228b 2013-07-01 kinaba: total += rec(I+1, cand, idx2, B); 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: // Use nonexisting. 4c0c9a228b 2013-07-01 kinaba: if(B > head.size()) { 4c0c9a228b 2013-07-01 kinaba: vector<int> idx2; 4c0c9a228b 2013-07-01 kinaba: for(int k=0; k<idx.size(); ++k) 4c0c9a228b 2013-07-01 kinaba: if(cand[idx[k]][I]==ANY) 4c0c9a228b 2013-07-01 kinaba: idx2.push_back(idx[k]); 4c0c9a228b 2013-07-01 kinaba: total += rec(I+1, cand, idx2, B) * (B - head.size()); 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: return total; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: }; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: // BEGIN CUT HERE 4c0c9a228b 2013-07-01 kinaba: #include <ctime> 4c0c9a228b 2013-07-01 kinaba: double start_time; string timer() 4c0c9a228b 2013-07-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4c0c9a228b 2013-07-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4c0c9a228b 2013-07-01 kinaba: { os << "{ "; 4c0c9a228b 2013-07-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4c0c9a228b 2013-07-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4c0c9a228b 2013-07-01 kinaba: void verify_case(const int& Expected, const int& Received) { 4c0c9a228b 2013-07-01 kinaba: bool ok = (Expected == Received); 4c0c9a228b 2013-07-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4c0c9a228b 2013-07-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4c0c9a228b 2013-07-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4c0c9a228b 2013-07-01 kinaba: #define END verify_case(_, SimilarSequences().count(seq, bound));} 4c0c9a228b 2013-07-01 kinaba: int main(){ 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: CASE(0) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = {1, 1}; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = 3; 4c0c9a228b 2013-07-01 kinaba: int _ = 5; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(1) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = {1, 2}; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = 2; 4c0c9a228b 2013-07-01 kinaba: int _ = 4; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(2) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = {999999999}; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = 1000000000; 4c0c9a228b 2013-07-01 kinaba: int _ = 1000000000; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(3) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = {1, 2, 3, 4, 5}; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = 5; 4c0c9a228b 2013-07-01 kinaba: int _ = 97; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(4) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = {5, 8, 11, 12, 4, 1, 7, 9}; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = 1000000000; 4c0c9a228b 2013-07-01 kinaba: int _ = 999999363; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: /* 4c0c9a228b 2013-07-01 kinaba: CASE(5) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = ; 4c0c9a228b 2013-07-01 kinaba: int _ = ; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(6) 4c0c9a228b 2013-07-01 kinaba: int seq_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 4c0c9a228b 2013-07-01 kinaba: int bound = ; 4c0c9a228b 2013-07-01 kinaba: int _ = ; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: */ 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: // END CUT HERE