69a17ebf8a 2021-07-08 kinaba: #include <iostream> 69a17ebf8a 2021-07-08 kinaba: #include <sstream> 69a17ebf8a 2021-07-08 kinaba: #include <iomanip> 69a17ebf8a 2021-07-08 kinaba: #include <vector> 69a17ebf8a 2021-07-08 kinaba: #include <string> 69a17ebf8a 2021-07-08 kinaba: #include <map> 69a17ebf8a 2021-07-08 kinaba: #include <set> 69a17ebf8a 2021-07-08 kinaba: #include <algorithm> 69a17ebf8a 2021-07-08 kinaba: #include <numeric> 69a17ebf8a 2021-07-08 kinaba: #include <iterator> 69a17ebf8a 2021-07-08 kinaba: #include <functional> 69a17ebf8a 2021-07-08 kinaba: #include <complex> 69a17ebf8a 2021-07-08 kinaba: #include <queue> 69a17ebf8a 2021-07-08 kinaba: #include <stack> 69a17ebf8a 2021-07-08 kinaba: #include <cmath> 69a17ebf8a 2021-07-08 kinaba: #include <cassert> 69a17ebf8a 2021-07-08 kinaba: #include <tuple> 69a17ebf8a 2021-07-08 kinaba: using namespace std; 69a17ebf8a 2021-07-08 kinaba: typedef long long LL; 69a17ebf8a 2021-07-08 kinaba: typedef complex<double> CMP; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: static const unsigned MODVAL = 1000000007; 69a17ebf8a 2021-07-08 kinaba: struct mint 69a17ebf8a 2021-07-08 kinaba: { 69a17ebf8a 2021-07-08 kinaba: unsigned val; 69a17ebf8a 2021-07-08 kinaba: mint() :val(0) {} 69a17ebf8a 2021-07-08 kinaba: mint(int x) :val(x%MODVAL) {} 69a17ebf8a 2021-07-08 kinaba: mint(unsigned x) :val(x%MODVAL) {} 69a17ebf8a 2021-07-08 kinaba: mint(LL x) :val(x%MODVAL) {} 69a17ebf8a 2021-07-08 kinaba: }; 69a17ebf8a 2021-07-08 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val + y.val; } 69a17ebf8a 2021-07-08 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; } 69a17ebf8a 2021-07-08 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 69a17ebf8a 2021-07-08 kinaba: mint operator+(mint x, mint y) { return x += y; } 69a17ebf8a 2021-07-08 kinaba: mint operator-(mint x, mint y) { return x -= y; } 69a17ebf8a 2021-07-08 kinaba: mint operator*(mint x, mint y) { return x *= y; } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; } 69a17ebf8a 2021-07-08 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); } 69a17ebf8a 2021-07-08 kinaba: mint operator/(mint x, mint y) { return x /= y; } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: vector<mint> FAC_(1, 1); 69a17ebf8a 2021-07-08 kinaba: mint FAC(LL n) { while (FAC_.size() <= n) FAC_.push_back(FAC_.back()*LL(FAC_.size())); return FAC_[n]; } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: // nCk :: O(log MODVAL) time, O(n) space. 69a17ebf8a 2021-07-08 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n - k)); } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: class AnnoyingPasswords { public: 69a17ebf8a 2021-07-08 kinaba: int count(int U, int L, int D) 69a17ebf8a 2021-07-08 kinaba: { 69a17ebf8a 2021-07-08 kinaba: return (solve(-1,U,L,D) * FAC(26) / FAC(26 - U) * FAC(26) / FAC(26 - L) * FAC(10) / FAC(10 - D)).val; 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: int solve(int p, int U, int L, int D) { 69a17ebf8a 2021-07-08 kinaba: tuple<int, int, int, int> key(p, U, L, D); 69a17ebf8a 2021-07-08 kinaba: if (memo.count(key)) 69a17ebf8a 2021-07-08 kinaba: return memo[key]; 69a17ebf8a 2021-07-08 kinaba: if (U == 0 && L == 0 && D == 0) 69a17ebf8a 2021-07-08 kinaba: return 1; 69a17ebf8a 2021-07-08 kinaba: mint ans = 0; 69a17ebf8a 2021-07-08 kinaba: if (p != 0 && U != 0) 69a17ebf8a 2021-07-08 kinaba: ans += solve(0, U - 1, L, D); 69a17ebf8a 2021-07-08 kinaba: if (p != 1 && L != 0) 69a17ebf8a 2021-07-08 kinaba: ans += solve(1, U, L - 1, D); 69a17ebf8a 2021-07-08 kinaba: if (p != 2 && D != 0) 69a17ebf8a 2021-07-08 kinaba: ans += solve(2, U, L, D - 1); 69a17ebf8a 2021-07-08 kinaba: return memo[key] = ans.val; 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: map<tuple<int, int, int, int>, int> memo; 69a17ebf8a 2021-07-08 kinaba: }; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: // BEGIN CUT HERE 69a17ebf8a 2021-07-08 kinaba: #include <ctime> 69a17ebf8a 2021-07-08 kinaba: double start_time; string timer() 69a17ebf8a 2021-07-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 69a17ebf8a 2021-07-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 69a17ebf8a 2021-07-08 kinaba: { os << "{ "; 69a17ebf8a 2021-07-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 69a17ebf8a 2021-07-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 69a17ebf8a 2021-07-08 kinaba: void verify_case(const int& Expected, const int& Received) { 69a17ebf8a 2021-07-08 kinaba: bool ok = (Expected == Received); 69a17ebf8a 2021-07-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 69a17ebf8a 2021-07-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 69a17ebf8a 2021-07-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 69a17ebf8a 2021-07-08 kinaba: #define END verify_case(_, AnnoyingPasswords().count(U, L, D));} 69a17ebf8a 2021-07-08 kinaba: int main(){ 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: CASE(0) 69a17ebf8a 2021-07-08 kinaba: int U = 4; 69a17ebf8a 2021-07-08 kinaba: int L = 1; 69a17ebf8a 2021-07-08 kinaba: int D = 1; 69a17ebf8a 2021-07-08 kinaba: int _ = 0; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(1) 69a17ebf8a 2021-07-08 kinaba: int U = 5; 69a17ebf8a 2021-07-08 kinaba: int L = 0; 69a17ebf8a 2021-07-08 kinaba: int D = 4; 69a17ebf8a 2021-07-08 kinaba: int _ = 783743727; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(2) 69a17ebf8a 2021-07-08 kinaba: int U = 1; 69a17ebf8a 2021-07-08 kinaba: int L = 1; 69a17ebf8a 2021-07-08 kinaba: int D = 1; 69a17ebf8a 2021-07-08 kinaba: int _ = 40560; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(3) 69a17ebf8a 2021-07-08 kinaba: int U = 2; 69a17ebf8a 2021-07-08 kinaba: int L = 2; 69a17ebf8a 2021-07-08 kinaba: int D = 3; 69a17ebf8a 2021-07-08 kinaba: int _ = 559599923; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(4) 69a17ebf8a 2021-07-08 kinaba: int U = 0; 69a17ebf8a 2021-07-08 kinaba: int L = 0; 69a17ebf8a 2021-07-08 kinaba: int D = 0; 69a17ebf8a 2021-07-08 kinaba: int _ = 1; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(5) 69a17ebf8a 2021-07-08 kinaba: int U = 26; 69a17ebf8a 2021-07-08 kinaba: int L = 26; 69a17ebf8a 2021-07-08 kinaba: int D = 10; 69a17ebf8a 2021-07-08 kinaba: int _ = 0; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(6) 69a17ebf8a 2021-07-08 kinaba: int U = 0; 69a17ebf8a 2021-07-08 kinaba: int L = 0; 69a17ebf8a 2021-07-08 kinaba: int D = 0; 69a17ebf8a 2021-07-08 kinaba: int _ = 1; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: // END CUT HERE