Artifact Content
Not logged in

Artifact b366e6f6edd62cddef172cc5ce77f63ebabe079a


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

static const int MODVAL = 1000000007;
struct mint
{
	int val;
	mint():val(0){}
	mint(int    x):val(x%MODVAL) {}
	mint(size_t 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 = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator*(mint x, mint y) { return x*=y; }

template<typename T>
vector<T> MATMUL(const vector< vector<T> >& a, const vector<T>& v)
{
	int N = a.size();
	vector<T> u(N);
	for(int i=0; i<N; ++i)
	for(int j=0; j<N; ++j)
		u[i] += a[i][j]*v[j];
	return u;
}

template<typename T>
vector< vector<T> > MATMUL(const vector< vector<T> >& a, const vector< vector<T> >& b)
{
	int N = a.size();
	vector< vector<T> > c(N, vector<T>(N));
	for(int i=0; i<N; ++i)
	for(int j=0; j<N; ++j)
	for(int k=0; k<N; ++k)
		c[i][j] += a[i][k]*b[k][j];
	return c;
}

template<typename T>
vector< vector<T> > MATPOW(vector< vector<T> > a, LL e)
{
	int N = a.size();
	vector< vector<T> > c(N, vector<T>(N));
	for(int i=0; i<N; ++i) c[i][i] = 1;
	for(; e; e>>=1) {
		if(e&1)
			c = MATMUL(c, a);
		a = MATMUL(a, a);
	}
	return c;
}

class ConversionMachine { public:
	int countAll(string word1, string word2, vector <int> costs, int maxCost)
	{
		int d[3] = {};
		LL minCost = 0;
		for(int i=0; i<word1.size(); ++i) {
			int dd=0;
			while( word1[i] != word2[i] ) {
				dd      += 1;
				minCost += costs[word1[i]-'a'];
				word1[i] = (word1[i]+1-'a')%3 + 'a';
			}
			d[dd] += 1;
		}

		if( minCost > maxCost )
			return 0;
		LL c3 = accumulate(costs.begin(), costs.end(), 0LL);
		return solve(d, (maxCost-minCost)/c3*3+d[1]*1+d[2]*2).val;
	}

	mint solve(int d[3], int rem)
	{
		int n = d[0]+d[1]+d[2];

		map<pair<int,int>, int> enc;
		for(int b=0,id=0; b<=n; ++b)
		for(int c=0; b+c<=n; ++c,++id)
			enc[make_pair(b,c)] = id;
		int D = enc.size();

		vector< vector<mint> > M(D+1, vector<mint>(D+1));
		for(int b=0; b<=n; ++b)
		for(int c=0; b+c<=n; ++c)
		{
			int a = n-b-c;
			if(a) {
				M[enc[make_pair(b,c+1)]][enc[make_pair(b,c)]] = a;
			}
			if(b) {
				M[enc[make_pair(b-1,c)]][enc[make_pair(b,c)]] = b;
				if(b-1==0 && c==0)
					M.back()[enc[make_pair(b,c)]] = b;
			}
			if(c) {
				M[enc[make_pair(b+1,c-1)]][enc[make_pair(b,c)]] = c;
			}
		}
		M.back().back() = 1;

		vector<mint> V(D+1);
		V[enc[make_pair(d[1],d[2])]] = 1;
		V.back() = V[0];

		return MATMUL(MATPOW(M, rem), V).back();
	}
};

// 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(_, ConversionMachine().countAll(word1, word2, costs, maxCost));}
int main(){

CASE(0)
	string word1 = "a"; 
	string word2 = "b"; 
	int costs_[] = {100,2,3};
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = 205; 
	int _ = 2; 
END
CASE(1)
	string word1 = "abcba"; 
	string word2 = "abcba"; 
	int costs_[] = {67,23,43};
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = 0; 
	int _ = 1; 
END
CASE(2)
	string word1 = "cccccccc"; 
	string word2 = "aaaaaaaa"; 
	int costs_[] = {10000000,1,1};
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = 100; 
	int _ = 40320; 
END
CASE(3)
	string word1 = "aa"; 
	string word2 = "cc"; 
	int costs_[] = {1,10,100};
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = 1787; 
	int _ = 123611681; 
END
/*
CASE(4)
	string word1 = ; 
	string word2 = ; 
	int costs_[] = ;
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = ; 
	int _ = ; 
END
CASE(5)
	string word1 = ; 
	string word2 = ; 
	int costs_[] = ;
	  vector <int> costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); 
	int maxCost = ; 
	int _ = ; 
END
*/
}
// END CUT HERE