c81888b809 2012-08-06 kinaba: #include <iostream> c81888b809 2012-08-06 kinaba: #include <sstream> c81888b809 2012-08-06 kinaba: #include <iomanip> c81888b809 2012-08-06 kinaba: #include <vector> c81888b809 2012-08-06 kinaba: #include <string> c81888b809 2012-08-06 kinaba: #include <map> c81888b809 2012-08-06 kinaba: #include <set> c81888b809 2012-08-06 kinaba: #include <algorithm> c81888b809 2012-08-06 kinaba: #include <numeric> c81888b809 2012-08-06 kinaba: #include <iterator> c81888b809 2012-08-06 kinaba: #include <functional> c81888b809 2012-08-06 kinaba: #include <complex> c81888b809 2012-08-06 kinaba: #include <queue> c81888b809 2012-08-06 kinaba: #include <stack> c81888b809 2012-08-06 kinaba: #include <cmath> c81888b809 2012-08-06 kinaba: #include <cassert> c81888b809 2012-08-06 kinaba: using namespace std; c81888b809 2012-08-06 kinaba: typedef long long LL; c81888b809 2012-08-06 kinaba: typedef long double LD; c81888b809 2012-08-06 kinaba: typedef complex<LD> CMP; c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: static const int MODVAL = 1000000007; c81888b809 2012-08-06 kinaba: struct mint c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int val; c81888b809 2012-08-06 kinaba: mint():val(0){} c81888b809 2012-08-06 kinaba: mint(int x):val(x%MODVAL) {} c81888b809 2012-08-06 kinaba: mint(size_t x):val(x%MODVAL) {} c81888b809 2012-08-06 kinaba: mint(LL x):val(x%MODVAL) {} c81888b809 2012-08-06 kinaba: }; c81888b809 2012-08-06 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } c81888b809 2012-08-06 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } c81888b809 2012-08-06 kinaba: mint operator+(mint x, mint y) { return x+=y; } c81888b809 2012-08-06 kinaba: mint operator*(mint x, mint y) { return x*=y; } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: template<typename T> c81888b809 2012-08-06 kinaba: vector<T> MATMUL(const vector< vector<T> >& a, const vector<T>& v) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int N = a.size(); c81888b809 2012-08-06 kinaba: vector<T> u(N); c81888b809 2012-08-06 kinaba: for(int i=0; i<N; ++i) c81888b809 2012-08-06 kinaba: for(int j=0; j<N; ++j) c81888b809 2012-08-06 kinaba: u[i] += a[i][j]*v[j]; c81888b809 2012-08-06 kinaba: return u; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: template<typename T> c81888b809 2012-08-06 kinaba: vector< vector<T> > MATMUL(const vector< vector<T> >& a, const vector< vector<T> >& b) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int N = a.size(); c81888b809 2012-08-06 kinaba: vector< vector<T> > c(N, vector<T>(N)); c81888b809 2012-08-06 kinaba: for(int i=0; i<N; ++i) c81888b809 2012-08-06 kinaba: for(int j=0; j<N; ++j) c81888b809 2012-08-06 kinaba: for(int k=0; k<N; ++k) c81888b809 2012-08-06 kinaba: c[i][j] += a[i][k]*b[k][j]; c81888b809 2012-08-06 kinaba: return c; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: template<typename T> c81888b809 2012-08-06 kinaba: vector< vector<T> > MATPOW(vector< vector<T> > a, LL e) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int N = a.size(); c81888b809 2012-08-06 kinaba: vector< vector<T> > c(N, vector<T>(N)); c81888b809 2012-08-06 kinaba: for(int i=0; i<N; ++i) c[i][i] = 1; c81888b809 2012-08-06 kinaba: for(; e; e>>=1) { c81888b809 2012-08-06 kinaba: if(e&1) c81888b809 2012-08-06 kinaba: c = MATMUL(c, a); c81888b809 2012-08-06 kinaba: a = MATMUL(a, a); c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: return c; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: class ConversionMachine { public: c81888b809 2012-08-06 kinaba: int countAll(string word1, string word2, vector <int> costs, int maxCost) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int d[3] = {}; c81888b809 2012-08-06 kinaba: LL minCost = 0; c81888b809 2012-08-06 kinaba: for(int i=0; i<word1.size(); ++i) { c81888b809 2012-08-06 kinaba: int dd=0; c81888b809 2012-08-06 kinaba: while( word1[i] != word2[i] ) { c81888b809 2012-08-06 kinaba: dd += 1; c81888b809 2012-08-06 kinaba: minCost += costs[word1[i]-'a']; c81888b809 2012-08-06 kinaba: word1[i] = (word1[i]+1-'a')%3 + 'a'; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: d[dd] += 1; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: if( minCost > maxCost ) c81888b809 2012-08-06 kinaba: return 0; c81888b809 2012-08-06 kinaba: LL c3 = accumulate(costs.begin(), costs.end(), 0LL); c81888b809 2012-08-06 kinaba: return solve(d, (maxCost-minCost)/c3*3+d[1]*1+d[2]*2).val; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: mint solve(int d[3], int rem) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int n = d[0]+d[1]+d[2]; c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: map<pair<int,int>, int> enc; c81888b809 2012-08-06 kinaba: for(int b=0,id=0; b<=n; ++b) c81888b809 2012-08-06 kinaba: for(int c=0; b+c<=n; ++c,++id) c81888b809 2012-08-06 kinaba: enc[make_pair(b,c)] = id; c81888b809 2012-08-06 kinaba: int D = enc.size(); c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: vector< vector<mint> > M(D+1, vector<mint>(D+1)); c81888b809 2012-08-06 kinaba: for(int b=0; b<=n; ++b) c81888b809 2012-08-06 kinaba: for(int c=0; b+c<=n; ++c) c81888b809 2012-08-06 kinaba: { c81888b809 2012-08-06 kinaba: int a = n-b-c; c81888b809 2012-08-06 kinaba: if(a) { c81888b809 2012-08-06 kinaba: M[enc[make_pair(b,c+1)]][enc[make_pair(b,c)]] = a; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: if(b) { c81888b809 2012-08-06 kinaba: M[enc[make_pair(b-1,c)]][enc[make_pair(b,c)]] = b; c81888b809 2012-08-06 kinaba: if(b-1==0 && c==0) c81888b809 2012-08-06 kinaba: M.back()[enc[make_pair(b,c)]] = b; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: if(c) { c81888b809 2012-08-06 kinaba: M[enc[make_pair(b+1,c-1)]][enc[make_pair(b,c)]] = c; c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: M.back().back() = 1; c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: vector<mint> V(D+1); c81888b809 2012-08-06 kinaba: V[enc[make_pair(d[1],d[2])]] = 1; c81888b809 2012-08-06 kinaba: V.back() = V[0]; c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: return MATMUL(MATPOW(M, rem), V).back(); c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: }; c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: // BEGIN CUT HERE c81888b809 2012-08-06 kinaba: #include <ctime> c81888b809 2012-08-06 kinaba: double start_time; string timer() c81888b809 2012-08-06 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c81888b809 2012-08-06 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c81888b809 2012-08-06 kinaba: { os << "{ "; c81888b809 2012-08-06 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c81888b809 2012-08-06 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c81888b809 2012-08-06 kinaba: void verify_case(const int& Expected, const int& Received) { c81888b809 2012-08-06 kinaba: bool ok = (Expected == Received); c81888b809 2012-08-06 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c81888b809 2012-08-06 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c81888b809 2012-08-06 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c81888b809 2012-08-06 kinaba: #define END verify_case(_, ConversionMachine().countAll(word1, word2, costs, maxCost));} c81888b809 2012-08-06 kinaba: int main(){ c81888b809 2012-08-06 kinaba: c81888b809 2012-08-06 kinaba: CASE(0) c81888b809 2012-08-06 kinaba: string word1 = "a"; c81888b809 2012-08-06 kinaba: string word2 = "b"; c81888b809 2012-08-06 kinaba: int costs_[] = {100,2,3}; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = 205; c81888b809 2012-08-06 kinaba: int _ = 2; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: CASE(1) c81888b809 2012-08-06 kinaba: string word1 = "abcba"; c81888b809 2012-08-06 kinaba: string word2 = "abcba"; c81888b809 2012-08-06 kinaba: int costs_[] = {67,23,43}; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = 0; c81888b809 2012-08-06 kinaba: int _ = 1; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: CASE(2) c81888b809 2012-08-06 kinaba: string word1 = "cccccccc"; c81888b809 2012-08-06 kinaba: string word2 = "aaaaaaaa"; c81888b809 2012-08-06 kinaba: int costs_[] = {10000000,1,1}; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = 100; c81888b809 2012-08-06 kinaba: int _ = 40320; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: CASE(3) c81888b809 2012-08-06 kinaba: string word1 = "aa"; c81888b809 2012-08-06 kinaba: string word2 = "cc"; c81888b809 2012-08-06 kinaba: int costs_[] = {1,10,100}; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = 1787; c81888b809 2012-08-06 kinaba: int _ = 123611681; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: /* c81888b809 2012-08-06 kinaba: CASE(4) c81888b809 2012-08-06 kinaba: string word1 = ; c81888b809 2012-08-06 kinaba: string word2 = ; c81888b809 2012-08-06 kinaba: int costs_[] = ; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = ; c81888b809 2012-08-06 kinaba: int _ = ; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: CASE(5) c81888b809 2012-08-06 kinaba: string word1 = ; c81888b809 2012-08-06 kinaba: string word2 = ; c81888b809 2012-08-06 kinaba: int costs_[] = ; c81888b809 2012-08-06 kinaba: vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); c81888b809 2012-08-06 kinaba: int maxCost = ; c81888b809 2012-08-06 kinaba: int _ = ; c81888b809 2012-08-06 kinaba: END c81888b809 2012-08-06 kinaba: */ c81888b809 2012-08-06 kinaba: } c81888b809 2012-08-06 kinaba: // END CUT HERE