Artifact b01c730886ab7acaebb68fae405b9120661f356e
#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>
#include <cstring>
#include <cctype>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
static const LL MODVAL = 1234567891;
class TheEncryptionDivOne { public:
map<vector<int>, LL> memo;
int count(string msg, string encMsg)
{
// accumulate unused characters
char cs[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
set<char> ori(cs+0, cs+52), enc(cs+0, cs+52);
for(int i=0; i<msg.size(); ++i)
ori.erase(msg[i]), enc.erase(encMsg[i]);
// obviously-invalid cases
for(int i=0; i<msg.size(); ++i)
if( toupper(msg[i]) == toupper(encMsg[i]) )
return 0;
if( ori.size() != enc.size() )
return 0;
// count
vector<int> s(9);
for(char c='a'; c<='z'; ++c)
{
int oc = ori.count(c)+ori.count(c+'A'-'a');
int ec = enc.count(c)+enc.count(c+'A'-'a');
s[oc*3+ec] ++;
}
return rec(s);
}
LL rec( const vector<int>& s )
{
if( memo.count(s) )
return memo[s];
for(int a=1; a<=2; ++a)
for(int b=0; b<=2; ++b) if( s[a*3+b] )
{
LL val = 0;
for(int c=0; c<=2; ++c)
for(int d=1; d<=2; ++d) if( s[c*3+d] )
{
vector<int> t = s;
t[a*3+b] --;
if( LL choices = t[c*3+d]*d )
{
t[(a-1)*3+b] ++;
t[c*3+d] --;
t[c*3+(d-1)] ++;
val += choices * rec(t);
}
}
return memo[s] = val%MODVAL;
}
return 1;
}
};
// 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(_, TheEncryptionDivOne().count(msg, encMsg));}
int main(){
CASE(0)
string msg = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX";
string encMsg = "cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int _ = 2;
END
CASE(1)
string msg = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX";
string encMsg = "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY";
int _ = 1;
END
CASE(2)
string msg = "topcoder";
string encMsg = "TOPCODER";
int _ = 0;
END
CASE(3)
string msg = "thisisaveryhardproblem";
string encMsg = "nobodywillsolveittoday";
int _ = 0;
END
CASE(4)
string msg = "a";
string encMsg = "W";
int _ = 710479617;
END
CASE(5)
string msg = "aMgqyMOaWSyaAxOyvOaRAEajOOAWWAqqAAj";
string encMsg = "qKYEnKaqMgnqGWanlaqkGdquaaGMMGEEGGu";
int _ = 650838813;
END
}
// END CUT HERE