#include #include #include #include #include #include #include using namespace std; template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } int main() { vector to(7, 7); array SCALE = {5, 5, 5, 5, 5, 5, 5, 25}; array start = {4, 4, 4, 4, 4, 4, 4, 24}; { string s; cin >> s; for(int i = 0; i < 7; i++) { if(s[i] == 'A') to[i] = 0; else if(s[i] >= '2' && s[i] <= '7') to[i] = s[i]-'1'; start[to[i]]--; SCALE[to[i]]--; } } vector> dp(127); auto dfs = y_combinator([&](auto self, int curr, int have) -> double { if(curr == 127) return 0; if(dp[curr].count(have)) return dp[curr][have]; array materialize; int tot = 0; { int now = have; for(int i = 7; i >= 0; i--) { materialize[i] = now % SCALE[i]; now /= SCALE[i]; tot += materialize[i]; } } { int orig = 0; for(int i = 0; i < 8; i++) { orig *= SCALE[i]; orig += materialize[i]; } assert(orig == have); } dp[curr][have] = 0; for(int i = 0; i < 8; i++) { if(materialize[i] == 0) continue; int ncurr = curr; materialize[i]--; { int x = i; while(x >= 0 && x < 7 && (ncurr&(1<