982758e5e4 2011-11-29 kinaba: #include <iostream> 982758e5e4 2011-11-29 kinaba: #include <sstream> 982758e5e4 2011-11-29 kinaba: #include <iomanip> 982758e5e4 2011-11-29 kinaba: #include <vector> 982758e5e4 2011-11-29 kinaba: #include <string> 982758e5e4 2011-11-29 kinaba: #include <map> 982758e5e4 2011-11-29 kinaba: #include <set> 982758e5e4 2011-11-29 kinaba: #include <algorithm> 982758e5e4 2011-11-29 kinaba: #include <numeric> 982758e5e4 2011-11-29 kinaba: #include <iterator> 982758e5e4 2011-11-29 kinaba: #include <functional> 982758e5e4 2011-11-29 kinaba: #include <complex> 982758e5e4 2011-11-29 kinaba: #include <queue> 982758e5e4 2011-11-29 kinaba: #include <stack> 982758e5e4 2011-11-29 kinaba: #include <cmath> 982758e5e4 2011-11-29 kinaba: #include <cassert> 982758e5e4 2011-11-29 kinaba: #include <cstring> 982758e5e4 2011-11-29 kinaba: #ifdef __GNUC__ 982758e5e4 2011-11-29 kinaba: #include <ext/hash_map> 982758e5e4 2011-11-29 kinaba: #define unordered_map __gnu_cxx::hash_map 982758e5e4 2011-11-29 kinaba: #else 982758e5e4 2011-11-29 kinaba: #include <unordered_map> 982758e5e4 2011-11-29 kinaba: #endif 982758e5e4 2011-11-29 kinaba: using namespace std; 982758e5e4 2011-11-29 kinaba: typedef long long LL; 982758e5e4 2011-11-29 kinaba: typedef complex<double> CMP; 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: static const int MODVAL = 1000000009; // must be prime for op/ 982758e5e4 2011-11-29 kinaba: struct mint 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: int val; 982758e5e4 2011-11-29 kinaba: mint():val(0){} 982758e5e4 2011-11-29 kinaba: mint(int x):val(x%MODVAL) {} // x>=0 982758e5e4 2011-11-29 kinaba: mint(size_t x):val(x%MODVAL) {} // x>=0 982758e5e4 2011-11-29 kinaba: mint(LL x):val(x%MODVAL) {} // x>=0 982758e5e4 2011-11-29 kinaba: }; 982758e5e4 2011-11-29 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 982758e5e4 2011-11-29 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 982758e5e4 2011-11-29 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 982758e5e4 2011-11-29 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 982758e5e4 2011-11-29 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 982758e5e4 2011-11-29 kinaba: mint operator+(mint x, mint y) { return x+=y; } 982758e5e4 2011-11-29 kinaba: mint operator-(mint x, mint y) { return x-=y; } 982758e5e4 2011-11-29 kinaba: mint operator*(mint x, mint y) { return x*=y; } 982758e5e4 2011-11-29 kinaba: mint operator/(mint x, mint y) { return x/=y; } 982758e5e4 2011-11-29 kinaba: vector<mint> FAC_(1,1); 982758e5e4 2011-11-29 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 982758e5e4 2011-11-29 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: class AxonometricProjection { public: 982758e5e4 2011-11-29 kinaba: int howManyWays(vector <int> left, vector <int> front) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: mint ans = 1; 982758e5e4 2011-11-29 kinaba: sort(left.begin(), left.end()); 982758e5e4 2011-11-29 kinaba: sort(front.begin(), front.end()); 982758e5e4 2011-11-29 kinaba: ans *= perm(left); 982758e5e4 2011-11-29 kinaba: ans *= perm(front); 982758e5e4 2011-11-29 kinaba: for(int i=0, k=0; i<left.size() || k<front.size(); ) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: if( k<front.size() && (i==left.size() || front[k]<left[i]) ) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: // advance k 982758e5e4 2011-11-29 kinaba: int h = front[k]; 982758e5e4 2011-11-29 kinaba: int len = 0; 982758e5e4 2011-11-29 kinaba: while( k!=front.size() && front[k]==h ) 982758e5e4 2011-11-29 kinaba: ++k, ++len; 982758e5e4 2011-11-29 kinaba: ans *= choose(h, len); 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: else if( i<left.size() && (k==front.size() || left[i]<front[k]) ) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: // advance i 982758e5e4 2011-11-29 kinaba: int h = left[i]; 982758e5e4 2011-11-29 kinaba: int len = 0; 982758e5e4 2011-11-29 kinaba: while( i!=left.size() && left[i]==h ) 982758e5e4 2011-11-29 kinaba: ++i, ++len; 982758e5e4 2011-11-29 kinaba: ans *= choose(h, len); 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: else 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: // both 982758e5e4 2011-11-29 kinaba: int h = left[i]; 982758e5e4 2011-11-29 kinaba: assert( h == front[k] ); 982758e5e4 2011-11-29 kinaba: int len1 = 0, len2 = 0; 982758e5e4 2011-11-29 kinaba: while( i!=left.size() && left[i]==h ) 982758e5e4 2011-11-29 kinaba: ++i, ++len1; 982758e5e4 2011-11-29 kinaba: while( k!=front.size() && front[k]==h ) 982758e5e4 2011-11-29 kinaba: ++k, ++len2; 982758e5e4 2011-11-29 kinaba: ans *= choose2(h, len1, len2); 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: return ans.val; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: mint perm(const vector<int>& x) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: map<int,int> cat; 982758e5e4 2011-11-29 kinaba: for(int i=0; i<x.size(); ++i) 982758e5e4 2011-11-29 kinaba: cat[x[i]]++; 982758e5e4 2011-11-29 kinaba: mint r = FAC(x.size()); 982758e5e4 2011-11-29 kinaba: for(map<int,int>::iterator it=cat.begin(); it!=cat.end(); ++it) 982758e5e4 2011-11-29 kinaba: r /= FAC(it->second); 982758e5e4 2011-11-29 kinaba: return r; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: mint choose(int h, int n) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: mint r = 0; 982758e5e4 2011-11-29 kinaba: for(int k=1; k<=n; ++k) 982758e5e4 2011-11-29 kinaba: r += C(n, k) * POW(h, n-k); 982758e5e4 2011-11-29 kinaba: return r; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: mint choose2(int h, int n1, int n2) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: return POW(2, n1+n2) + h * choose(h,n1) * choose(h,n2); 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: }; 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: // BEGIN CUT HERE 982758e5e4 2011-11-29 kinaba: #include <ctime> 982758e5e4 2011-11-29 kinaba: double start_time; string timer() 982758e5e4 2011-11-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 982758e5e4 2011-11-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 982758e5e4 2011-11-29 kinaba: { os << "{ "; 982758e5e4 2011-11-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 982758e5e4 2011-11-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 982758e5e4 2011-11-29 kinaba: void verify_case(const int& Expected, const int& Received) { 982758e5e4 2011-11-29 kinaba: bool ok = (Expected == Received); 982758e5e4 2011-11-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 982758e5e4 2011-11-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 982758e5e4 2011-11-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 982758e5e4 2011-11-29 kinaba: #define END verify_case(_, AxonometricProjection().howManyWays(heightsOfLeftView, heightsOfFrontView));} 982758e5e4 2011-11-29 kinaba: int main(){ 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: CASE(0) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = {1,1}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = {1,1}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = 7; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(1) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = {50,50,50,50,524}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = {524,524}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = 104060401; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(2) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = {1,2,3,4,5,6,7,8,9,10}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = {1,2,3,4,5,6,7,8,9,10,11}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = 0; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(3) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = {5,2,4,1}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = {5,2,4,0,1}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = 429287; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(4) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = {5,2,4,52,24,524}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = {0,4,20,24,500,504,520,524}; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = 97065655; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: /* 982758e5e4 2011-11-29 kinaba: CASE(5) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = ; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = ; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = ; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(6) 982758e5e4 2011-11-29 kinaba: int heightsOfLeftView_[] = ; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfLeftView(heightsOfLeftView_, heightsOfLeftView_+sizeof(heightsOfLeftView_)/sizeof(*heightsOfLeftView_)); 982758e5e4 2011-11-29 kinaba: int heightsOfFrontView_[] = ; 982758e5e4 2011-11-29 kinaba: vector <int> heightsOfFrontView(heightsOfFrontView_, heightsOfFrontView_+sizeof(heightsOfFrontView_)/sizeof(*heightsOfFrontView_)); 982758e5e4 2011-11-29 kinaba: int _ = ; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: */ 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: // END CUT HERE