8bf237c40d 2011-10-04 kinaba: #include <iostream> 8bf237c40d 2011-10-04 kinaba: #include <sstream> 8bf237c40d 2011-10-04 kinaba: #include <iomanip> 8bf237c40d 2011-10-04 kinaba: #include <vector> 8bf237c40d 2011-10-04 kinaba: #include <string> 8bf237c40d 2011-10-04 kinaba: #include <map> 8bf237c40d 2011-10-04 kinaba: #include <set> 8bf237c40d 2011-10-04 kinaba: #include <algorithm> 8bf237c40d 2011-10-04 kinaba: #include <numeric> 8bf237c40d 2011-10-04 kinaba: #include <iterator> 8bf237c40d 2011-10-04 kinaba: #include <functional> 8bf237c40d 2011-10-04 kinaba: #include <complex> 8bf237c40d 2011-10-04 kinaba: #include <queue> 8bf237c40d 2011-10-04 kinaba: #include <stack> 8bf237c40d 2011-10-04 kinaba: #include <cmath> 8bf237c40d 2011-10-04 kinaba: #include <cassert> 8bf237c40d 2011-10-04 kinaba: #include <cstring> 8bf237c40d 2011-10-04 kinaba: using namespace std; 8bf237c40d 2011-10-04 kinaba: typedef long long LL; 8bf237c40d 2011-10-04 kinaba: typedef complex<double> CMP; 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: static const int MODVAL = 1000000007; 8bf237c40d 2011-10-04 kinaba: struct mint 8bf237c40d 2011-10-04 kinaba: { 8bf237c40d 2011-10-04 kinaba: int val; 8bf237c40d 2011-10-04 kinaba: mint():val(0){} 8bf237c40d 2011-10-04 kinaba: mint(int x):val(x%MODVAL) {} 8bf237c40d 2011-10-04 kinaba: mint(size_t x):val(x%MODVAL) {} 8bf237c40d 2011-10-04 kinaba: mint(LL x):val(x%MODVAL) {} 8bf237c40d 2011-10-04 kinaba: }; 8bf237c40d 2011-10-04 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 8bf237c40d 2011-10-04 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 8bf237c40d 2011-10-04 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 8bf237c40d 2011-10-04 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 8bf237c40d 2011-10-04 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 8bf237c40d 2011-10-04 kinaba: mint operator+(mint x, mint y) { return x+=y; } 8bf237c40d 2011-10-04 kinaba: mint operator-(mint x, mint y) { return x-=y; } 8bf237c40d 2011-10-04 kinaba: mint operator*(mint x, mint y) { return x*=y; } 8bf237c40d 2011-10-04 kinaba: mint operator/(mint x, mint y) { return x/=y; } 8bf237c40d 2011-10-04 kinaba: vector<mint> FAC_(1,1); 8bf237c40d 2011-10-04 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 8bf237c40d 2011-10-04 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: class SRMChallengePhase { public: 8bf237c40d 2011-10-04 kinaba: int countWays(vector <string> codersAttempted, vector <string> codersChallenged) 8bf237c40d 2011-10-04 kinaba: { 8bf237c40d 2011-10-04 kinaba: string at = accumulate(codersAttempted.begin(), codersAttempted.end(), string("")); 8bf237c40d 2011-10-04 kinaba: string sc = accumulate(codersChallenged.begin(), codersChallenged.end(), string("")); 8bf237c40d 2011-10-04 kinaba: const int N = at.size(); 8bf237c40d 2011-10-04 kinaba: int NN=0, NY=0, YN=0, YY=0; 8bf237c40d 2011-10-04 kinaba: for(int i=0; i<N; ++i) 8bf237c40d 2011-10-04 kinaba: if( at[i]=='N' ) 8bf237c40d 2011-10-04 kinaba: (sc[i]=='N' ? NN : NY) ++; 8bf237c40d 2011-10-04 kinaba: else 8bf237c40d 2011-10-04 kinaba: (sc[i]=='N' ? YN : YY) ++; 8bf237c40d 2011-10-04 kinaba: return solve(NN, NY, YN, YY); 8bf237c40d 2011-10-04 kinaba: } 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: int solve(int NN, int NY, int YN, int YY) 8bf237c40d 2011-10-04 kinaba: { 8bf237c40d 2011-10-04 kinaba: memo.clear(); 8bf237c40d 2011-10-04 kinaba: TOTAL = NN+NY+YN+YY; 8bf237c40d 2011-10-04 kinaba: return rec(YN, NY, YY).val; 8bf237c40d 2011-10-04 kinaba: } 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: map<pair<pair<int,int>,int>, mint> memo; 8bf237c40d 2011-10-04 kinaba: int TOTAL; 8bf237c40d 2011-10-04 kinaba: mint rec(int YN, int NY, int YY) 8bf237c40d 2011-10-04 kinaba: { 8bf237c40d 2011-10-04 kinaba: if(YN+YY < YY+NY) 8bf237c40d 2011-10-04 kinaba: return 0; 8bf237c40d 2011-10-04 kinaba: if(YN==0 && YY==0) 8bf237c40d 2011-10-04 kinaba: return 1; 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: pair<pair<int,int>,int> key(make_pair(YN,NY),YY); 8bf237c40d 2011-10-04 kinaba: if( memo.count(key) ) 8bf237c40d 2011-10-04 kinaba: return memo[key]; 8bf237c40d 2011-10-04 kinaba: mint ans = 0; 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: // YN, unsuc 8bf237c40d 2011-10-04 kinaba: if(YN) ans += rec(YN-1, NY, YY) * YN * (TOTAL-1); 8bf237c40d 2011-10-04 kinaba: // YN, suc 8bf237c40d 2011-10-04 kinaba: if(YN&&NY) ans += rec(YN-1, NY-1, YY) * YN * NY; 8bf237c40d 2011-10-04 kinaba: // YY, unsuc 8bf237c40d 2011-10-04 kinaba: if(YY) ans += rec(YN, NY+1, YY-1) * YY * (TOTAL-1); 8bf237c40d 2011-10-04 kinaba: // YY, suc 8bf237c40d 2011-10-04 kinaba: if(YY&&NY) ans += rec(YN, NY, YY-1) * YY * NY; 8bf237c40d 2011-10-04 kinaba: return memo[key] = ans; 8bf237c40d 2011-10-04 kinaba: } 8bf237c40d 2011-10-04 kinaba: }; 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: // BEGIN CUT HERE 8bf237c40d 2011-10-04 kinaba: #include <ctime> 8bf237c40d 2011-10-04 kinaba: double start_time; string timer() 8bf237c40d 2011-10-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 8bf237c40d 2011-10-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 8bf237c40d 2011-10-04 kinaba: { os << "{ "; 8bf237c40d 2011-10-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 8bf237c40d 2011-10-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 8bf237c40d 2011-10-04 kinaba: void verify_case(const int& Expected, const int& Received) { 8bf237c40d 2011-10-04 kinaba: bool ok = (Expected == Received); 8bf237c40d 2011-10-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 8bf237c40d 2011-10-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 8bf237c40d 2011-10-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 8bf237c40d 2011-10-04 kinaba: #define END verify_case(_, SRMChallengePhase().countWays(codersAttempted, codersChallenged));} 8bf237c40d 2011-10-04 kinaba: int main(){ 8bf237c40d 2011-10-04 kinaba: 8bf237c40d 2011-10-04 kinaba: CASE(0) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"YY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"NY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 1; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(1) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"YY", "NN"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"N", "NYY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 4; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(2) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"YNNN"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"NYY", "Y"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 0; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(3) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"N"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"N"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 1; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(4) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"YYY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"NNY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 24; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(5) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = {"YY", "N", "YYYY", "NN", "YYYYY"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = {"N", "YYYYY", "N", "Y", "N", "YYYY", "N"}; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = 807026001; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: /* 8bf237c40d 2011-10-04 kinaba: CASE(6) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = ; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = ; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = ; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: CASE(7) 8bf237c40d 2011-10-04 kinaba: string codersAttempted_[] = ; 8bf237c40d 2011-10-04 kinaba: vector <string> codersAttempted(codersAttempted_, codersAttempted_+sizeof(codersAttempted_)/sizeof(*codersAttempted_)); 8bf237c40d 2011-10-04 kinaba: string codersChallenged_[] = ; 8bf237c40d 2011-10-04 kinaba: vector <string> codersChallenged(codersChallenged_, codersChallenged_+sizeof(codersChallenged_)/sizeof(*codersChallenged_)); 8bf237c40d 2011-10-04 kinaba: int _ = ; 8bf237c40d 2011-10-04 kinaba: END 8bf237c40d 2011-10-04 kinaba: */ 8bf237c40d 2011-10-04 kinaba: } 8bf237c40d 2011-10-04 kinaba: // END CUT HERE