Artifact Content
Not logged in

Artifact 6dcf84ee0fbfd6820636108b45dc9315793260cb


#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;

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; }

mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); }
mint operator/(mint x, mint y) { return x/=y; }

vector<mint> FAC_(1,1);
mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }

mint P(LL n, LL k) {
	return k<0 || n<k ? 0 : FAC(n) / FAC(n-k);
}

// Tested: SRM 468 Lv2
template<typename T>
struct DP3x
{
	int N1, N2, N3;
	vector<T> data;
	DP3x(int, int N2, int N3, const T& t = T())
		: N1(2), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T) < (1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ i1&=1; return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3x& rhs)
		{ data.swap(rhs.data); }
};

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

class ColorfulBuilding { public:
	int count(vector <string> color1, vector <string> color2, int L)
	{
		string C1 = accumulate(color1.begin(), color1.end(), string());
		string C2 = accumulate(color2.begin(), color2.end(), string());
		int N = C1.size();
		vector<int> C;
		for(int i=0; i<N; ++i)
			C.push_back(to_i(C1[i])*52 + to_i(C2[i]));
		return solve(C, L).val;
	}

	static int to_i(char c)
	{
		return ('A'<=c && c<='Z' ? c-'A' : c-'a'+26);
	}

	mint solve(vector<int>& C, int L)
	{
		int N = C.size();

		DP3x<mint> dp(L+1, N, N+1); 
		for(int c=1; c<=L; ++c)
		{
			for(int x=0; x<N; ++x)
			{
				dp(c,x,1) = (c==1 ? 1 : 0);
				for(int i=2; i<=x+1; ++i)
				{
					mint sum = 0;
					for(int y=0; y<x; ++y) {
						int cc = (C[x]==C[y] ? c : c-1);
						for(int k=1; k<=y+1 && k<=i-1; ++k)
							sum += dp(cc,y,k) * P(y+1-k, i-1-k);
					}
					dp(c,x,i) = sum;
				}
			}
		}

		mint ans = 0;
		for(int i=1; i<=N; ++i)
			ans += dp(L, N-1, i) * P(N-i, N-i);
		return ans;
	}
};

// 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(_, ColorfulBuilding().count(color1, color2, L));}
int main(){

CASE(0)
	string color1_[] = {"aaba"};
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = {"aaaa"};
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = 3; 
	int _ = 6; 
END
CASE(1)
	string color1_[] = {"aaba"};
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = {"aaba"};
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = 4; 
	int _ = 0; 
END
CASE(2)
	string color1_[] = {"ab", "ba", "a", "aab"};
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = {"bb", "ba", "a", "aba"};
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = 5; 
	int _ = 432; 
END
CASE(3)
	string color1_[] = {"xxxxxxxxxxxxxxxxxxxx",
 "xxxxxxxxxxxxxxxxxxxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOxxxxxxxxxxxxxxxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxxxxxxxxxxxxxxxOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxxxxxxxxxxxxxxxxxxx",
 "xxxxxxxxxxxxxxxxxxxx"};
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = {"xxxxxxxxxxxxxxxxxxxx",
 "xxxxxxxxxxxxxxxxxxxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOxxxxxxxxxxxxOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxOOOOOOOOOOOOOOOOxx",
 "xxxxxxxxxxxxxxxxxxxx",
 "xxxxxxxxxxxxxxxxxxxx"};
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = 58; 
	int _ = 619787617; 
END
CASE(4)
	string color1_[] = {"SJXcabKTWeUXhwxGixFepQaQlFxrAedNAtVP",
 "gorBIkcTlOFbLDbFeElYAyAqszQdMbpxSRdE",
 "SQVHPehlNtesbJDflyGxMqvgzjXisRnqDcQO",
 "pIAEBwbmpPWCooQAujbGXFGLvXxTmExLARkf",
 "AFnWyWKVObfxDGCjTxdZaObgwdxlPKtIxaAd",
 "uznMpJVNjAofbHJjOrZeSHgS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KTWdyLi",
 "ODJLQPMdjDdTlxrfGsNmBfeGYjzeXApqKDhJ",
 "vlJkWMzdVQujKdLViQClOrJXMTBkuZEasFgg",
 "FAsbuzrLVIgaryqXBfuBRAbReleXSSgEKSvt"};
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = {"XAhlUHtfjOpOlQlowWppQcodMGUiqoLobxnD",
 "IDzGlKrHiVGdBjZxIcoxjwagbWieKvUwZrjg",
 "SkpkTVeIFeoNQzUUgmYAYskKONNZdgXxSiWd",
 "rXBGhTmqnvxkmqiutoDzPusDiOUggEFXGCzm",
 "fzrxVuTbFXunGbeEavjshmrIRlDorTuISCxn",
 "LsvIaxTkOBjcskiekmrKNNFEXqnfMNXLWqqu",
 "ekzxGIlbJPVEQPYAbTuMtZKsCiubDXDiBVzU",
 "hZuOCJxvBOYENGSFcUiKKAKfCdebutVmnyvB",
 "UveNkKRQmHnedrROQOpHJfrHjwcNLUShlDbn",
 "ptUkvbaGDryHRkYfHKLkSgVpjWCEcgazyxGK",
 "nVPYEqTTJsRPjzjfdOiULhnZPFeNcnbXaQlk",
 "IPXBXRhMQIkxpygsgbcRfMuvIcuzUPPHGOWX",
 "yWWlNyEyqZSIOXBFAybIuFpVqpvmKRaRFrAE",
 "EvBJVtHvKhjrFcmtpdBbFTdTVtXXZQKAglKT",
 "bCVjHzUvyINFkxXageZQMzCyNhcifACdJVDh",
 "lZITYcDSeIbLweyFtoMAfOQyBNupKlhcNpgo",
 "BduslNrJdWOUukYFFidEkMFaghfofpxVgvJd",
 "YrJpDZKqdjEPzdLsOQEdkcrBfNHPemXHokCW",
 "GjeUKSGjDlgKTyUGNrMQbBLxRUcgrWpkAwOD",
 "wgxTcswqdJHaDugNIRMvrhBsdDaJAssVbSRW",
 "qmVmqFEpvgGioMXDSFqEoQcDOAaUoGPEovSO",
 "KrukPlpfOhawaovCfteTSCIdLMrtImVtiMyQ",
 "ykwmxHsKMFzFHwcbyyedLvhZPnaNGqJMMCxd",
 "HUNYCXjNLQIFCLLGpCXHBCHLTxLynBxnHFbx",
 "uwjzbNbJepVFgMPUXVirxYHzExquBEtPmKju",
 "xXAxAbJePyUsVHeLytDvAxBGMRtnvCEiZZqe",
 "xMkQoIVxWPXPgaOYmDjTOXiMImVdzojERNxS",
 "dwICFwMAmdOIUxyAdXdshasnzwyhfnVWVqZJ",
 "etypXNVvSTEQvriGBZdSGmDEHhvpSqkFklCS",
 "YkxpFBCRYUueRcKaJUXVdaMoYMYEooPQVMOr",
 "DTrexDqclZNKdPuTRFHualJSFziCLPCZjpxo",
 "TfEijcAsSJPikkmBSbXMqYHAhPTcpcKVSkIX",
 "xKXHYPYMJxFpSbxltDKYuRiTkOLxpQKnXZPs",
 "YFYuvuYHfpFJcrLNIdlNfBxRnWdppsdalBkx",
 "NFTysBvNFjejdnlhRTclbcfGipNCxpFEOriY",
 "thkgVflJYmbUYbIlafNUMGePQWiZyYzYXvUR"}
;
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = 1200; 
	int _ = 396065426; 
END
/*
CASE(5)
	string color1_[] = ;
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = ;
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = ; 
	int _ = ; 
END
CASE(6)
	string color1_[] = ;
	  vector <string> color1(color1_, color1_+sizeof(color1_)/sizeof(*color1_)); 
	string color2_[] = ;
	  vector <string> color2(color2_, color2_+sizeof(color2_)/sizeof(*color2_)); 
	int L = ; 
	int _ = ; 
END
*/
}
// END CUT HERE