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: template<typename T> 4fd800b3a8 2011-02-23 kinaba: struct DP2 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int N1, N2; 4fd800b3a8 2011-02-23 kinaba: vector<T> data; 4fd800b3a8 2011-02-23 kinaba: DP2(int N1, int N2, const T& t = T()) 4fd800b3a8 2011-02-23 kinaba: : N1(N1), N2(N2), data(N1*N2, t) {} 4fd800b3a8 2011-02-23 kinaba: T& operator()(int i1, int i2) 4fd800b3a8 2011-02-23 kinaba: { return data[ (i1*N2)+i2 ]; } 4fd800b3a8 2011-02-23 kinaba: void swap(DP2& rhs) 4fd800b3a8 2011-02-23 kinaba: { data.swap(rhs.data); } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class NumbersAndMatches { public: 4fd800b3a8 2011-02-23 kinaba: long long differentNumbers(long long N, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string s; 4fd800b3a8 2011-02-23 kinaba: for(; N; N/=10) 4fd800b3a8 2011-02-23 kinaba: s = char('0' + N%10) + s; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: DP2<LL> dp(127,127); 4fd800b3a8 2011-02-23 kinaba: dp(0,0) = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: DP2<LL> dp2(127, 127); 4fd800b3a8 2011-02-23 kinaba: for(int tooMany=0; tooMany<=K; ++tooMany) 4fd800b3a8 2011-02-23 kinaba: for(int tooFew=0; tooFew<=K; ++tooFew) 4fd800b3a8 2011-02-23 kinaba: for(char c='0'; c<='9'; ++c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int tM=tooMany, tF=tooFew; 4fd800b3a8 2011-02-23 kinaba: diff(s[i], c, &tM, &tF); 4fd800b3a8 2011-02-23 kinaba: if( tM<=K && tF<=K ) 4fd800b3a8 2011-02-23 kinaba: dp2(tM,tF) += dp(tooMany,tooFew); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: dp.swap(dp2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int tMF=0; tMF<=K; ++tMF) 4fd800b3a8 2011-02-23 kinaba: ans += dp(tMF, tMF); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void diff(char a, char b, int* tF, int* tM) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const char* a7 = seven_seg(a); 4fd800b3a8 2011-02-23 kinaba: const char* b7 = seven_seg(b); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<7; ++i) 4fd800b3a8 2011-02-23 kinaba: if( a7[i] < b7[i] ) 4fd800b3a8 2011-02-23 kinaba: ++*tF; 4fd800b3a8 2011-02-23 kinaba: else if( a7[i] > b7[i] ) 4fd800b3a8 2011-02-23 kinaba: ++*tM; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: const char* seven_seg(char c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const char* ans[] = { 4fd800b3a8 2011-02-23 kinaba: "1110111", 4fd800b3a8 2011-02-23 kinaba: "0010011", 4fd800b3a8 2011-02-23 kinaba: "1011101", 4fd800b3a8 2011-02-23 kinaba: "1011011", 4fd800b3a8 2011-02-23 kinaba: "0111010", 4fd800b3a8 2011-02-23 kinaba: "1101011", 4fd800b3a8 2011-02-23 kinaba: "1101111", 4fd800b3a8 2011-02-23 kinaba: "1010010", 4fd800b3a8 2011-02-23 kinaba: "1111111", 4fd800b3a8 2011-02-23 kinaba: "1111011", 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: return ans[c-'0']; 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 long long& Expected, const long long& 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(_, NumbersAndMatches().differentNumbers(N, K));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: long long N = 10LL; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 4LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: long long N = 23LL; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 4LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: long long N = 66LL; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: long long _ = 15LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: long long N = 888888888LL; 4fd800b3a8 2011-02-23 kinaba: int K = 100; 4fd800b3a8 2011-02-23 kinaba: long long _ = 1LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: long long N = 444444444444444444LL; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: long long _ = 1LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: long long N = 1LL; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 2LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: long long N = 12345678901234567LL; 4fd800b3a8 2011-02-23 kinaba: int K = 126; 4fd800b3a8 2011-02-23 kinaba: long long _ = -1LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE