4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int MODVAL = 1000000009; 4fd800b3a8 2011-02-23 kinaba: struct mint 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int val; 4fd800b3a8 2011-02-23 kinaba: mint():val(0){} 4fd800b3a8 2011-02-23 kinaba: mint(int x):val(x%MODVAL) {} 4fd800b3a8 2011-02-23 kinaba: mint(LL x):val(x%MODVAL) {} 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: mint operator+(mint x, mint y) { return x.val+y.val; } 4fd800b3a8 2011-02-23 kinaba: mint operator-(mint x, mint y) { return x.val-y.val+MODVAL; } 4fd800b3a8 2011-02-23 kinaba: mint operator*(mint x, mint y) { return LL(x.val)*y.val; } 4fd800b3a8 2011-02-23 kinaba: mint POW(mint x, int e) { 4fd800b3a8 2011-02-23 kinaba: mint v = 1; 4fd800b3a8 2011-02-23 kinaba: for(;e;x=x*x,e>>=1) 4fd800b3a8 2011-02-23 kinaba: if(e&1) 4fd800b3a8 2011-02-23 kinaba: v=v*x; 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: mint operator/(mint x, mint y) { return x * POW(y, MODVAL-2); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<mint> FAC_(1,1); 4fd800b3a8 2011-02-23 kinaba: void FAC_INIT(int n) { for(int i=1; i<=n; ++i) FAC_.push_back( FAC_.back()*i ); } 4fd800b3a8 2011-02-23 kinaba: mint FAC(mint x) { return FAC_[x.val]; } 4fd800b3a8 2011-02-23 kinaba: mint C(mint n, mint k) { return k.val<0 || n.val<k.val ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Passwords { public: 4fd800b3a8 2011-02-23 kinaba: int countValid(int N, int L, int U, int D) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: FAC_INIT(200000); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Sigma_{D<=d<=N-U-L} [C(N,d) 10^d Sigma_{L<=l,U<=u,u+l=N-d}[ C(N-d,u) 26^u 26^l ] ] 4fd800b3a8 2011-02-23 kinaba: //= Sigma_{D<=d<=N-U-L} [C(N,d) 10^d 26^(N-d) Sigma_{U<=u<=N-d-L}[ C(N-d,u) ] ] 4fd800b3a8 2011-02-23 kinaba: // let f(N') = Sigma_{U<=u<=N'-L}[ C(N',u) ] 4fd800b3a8 2011-02-23 kinaba: // then by Pascal's triangle, f(N') = 2*f(N'-1)+C(N'-1,U-1)+C(N'-1,N'-L) 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: mint answer=0, f=C(U+L,U); 4fd800b3a8 2011-02-23 kinaba: for(int d=N-U-L; d>=D; --d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: answer = answer + C(N,d)*POW(10,d)*POW(26,N-d)*f; 4fd800b3a8 2011-02-23 kinaba: f = f*2 + C(N-d,U-1) + C(N-d, N-d-L+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return answer.val; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, Passwords().countValid(N, L, U, D));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int L = 0; 4fd800b3a8 2011-02-23 kinaba: int U = 0; 4fd800b3a8 2011-02-23 kinaba: int D = 2; 4fd800b3a8 2011-02-23 kinaba: int _ = 100; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int L = 1; 4fd800b3a8 2011-02-23 kinaba: int U = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 40560; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int N = 4; 4fd800b3a8 2011-02-23 kinaba: int L = 1; 4fd800b3a8 2011-02-23 kinaba: int U = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 5029440; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int L = 1; 4fd800b3a8 2011-02-23 kinaba: int U = 3; 4fd800b3a8 2011-02-23 kinaba: int D = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 818019214; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int L = 2; 4fd800b3a8 2011-02-23 kinaba: int U = 2; 4fd800b3a8 2011-02-23 kinaba: int D = 2; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int N = 199201; 4fd800b3a8 2011-02-23 kinaba: int L = 0; 4fd800b3a8 2011-02-23 kinaba: int U = 70000; 4fd800b3a8 2011-02-23 kinaba: int D = 0; 4fd800b3a8 2011-02-23 kinaba: int _ = 943311211; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int N = 200000; 4fd800b3a8 2011-02-23 kinaba: int L = 11; 4fd800b3a8 2011-02-23 kinaba: int U = 111; 4fd800b3a8 2011-02-23 kinaba: int D = 1111; 4fd800b3a8 2011-02-23 kinaba: int _ = 298235838; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE