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 <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: 4fd800b3a8 2011-02-23 kinaba: static const LL MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: int memo[10][11][1001][9]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class MegaCoolNumbers 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int count(int N, int A) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: memset(memo, -1, sizeof(memo)); 4fd800b3a8 2011-02-23 kinaba: return rec(1, 10, N, A); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int rec(int D, int F, int N, int A) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( A==0 ) return N==0 ? 1 : 0; 4fd800b3a8 2011-02-23 kinaba: if( N < A ) return 0; 4fd800b3a8 2011-02-23 kinaba: if( N==1 ) return 10-D - (F==10 ? 0 : 1); 4fd800b3a8 2011-02-23 kinaba: if( 10-D < A ) return 0; 4fd800b3a8 2011-02-23 kinaba: if( memo[D][F][N][A] != -1 ) 4fd800b3a8 2011-02-23 kinaba: return memo[D][F][N][A]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int d=D; d<=9; ++d) if(d!=F) 4fd800b3a8 2011-02-23 kinaba: for(int k=2; k<=N; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int q=0; d+(k-1)*q<=9; ++q) 4fd800b3a8 2011-02-23 kinaba: ans += rec(d+(k-1)*q, min(10, d+k*q), N-k, A-1); 4fd800b3a8 2011-02-23 kinaba: return memo[D][F][N][A] = int(ans % MODVAL); 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int N = 1; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 9; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 45; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int A = 2; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int A = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 7502; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int N = 1000; 4fd800b3a8 2011-02-23 kinaba: int A = 5; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 7502; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: