1c60f8bc27 2011-12-28 kinaba: #include <iostream> 1c60f8bc27 2011-12-28 kinaba: #include <sstream> 1c60f8bc27 2011-12-28 kinaba: #include <iomanip> 1c60f8bc27 2011-12-28 kinaba: #include <vector> 1c60f8bc27 2011-12-28 kinaba: #include <string> 1c60f8bc27 2011-12-28 kinaba: #include <map> 1c60f8bc27 2011-12-28 kinaba: #include <set> 1c60f8bc27 2011-12-28 kinaba: #include <algorithm> 1c60f8bc27 2011-12-28 kinaba: #include <numeric> 1c60f8bc27 2011-12-28 kinaba: #include <iterator> 1c60f8bc27 2011-12-28 kinaba: #include <functional> 1c60f8bc27 2011-12-28 kinaba: #include <complex> 1c60f8bc27 2011-12-28 kinaba: #include <queue> 1c60f8bc27 2011-12-28 kinaba: #include <stack> 1c60f8bc27 2011-12-28 kinaba: #include <cmath> 1c60f8bc27 2011-12-28 kinaba: #include <cassert> 1c60f8bc27 2011-12-28 kinaba: #include <cstring> 1c60f8bc27 2011-12-28 kinaba: #ifdef __GNUC__ 1c60f8bc27 2011-12-28 kinaba: #include <ext/hash_map> 1c60f8bc27 2011-12-28 kinaba: #define unordered_map __gnu_cxx::hash_map 1c60f8bc27 2011-12-28 kinaba: #else 1c60f8bc27 2011-12-28 kinaba: #include <unordered_map> 1c60f8bc27 2011-12-28 kinaba: #endif 1c60f8bc27 2011-12-28 kinaba: using namespace std; 1c60f8bc27 2011-12-28 kinaba: typedef long long LL; 1c60f8bc27 2011-12-28 kinaba: typedef complex<double> CMP; 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: class SumOfLuckiness { public: 1c60f8bc27 2011-12-28 kinaba: long long theSum(int A, int B) 1c60f8bc27 2011-12-28 kinaba: { 1c60f8bc27 2011-12-28 kinaba: return theSum(B) - theSum(A-1); 1c60f8bc27 2011-12-28 kinaba: } 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: LL theSum(LL X) 1c60f8bc27 2011-12-28 kinaba: { 1c60f8bc27 2011-12-28 kinaba: string S; {stringstream ss; ss<<X; ss>>S;} 1c60f8bc27 2011-12-28 kinaba: memo.clear(); 1c60f8bc27 2011-12-28 kinaba: return rec(S, 0, 0); 1c60f8bc27 2011-12-28 kinaba: } 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: LL rec(const string& S, int i, int dif) 1c60f8bc27 2011-12-28 kinaba: { 1c60f8bc27 2011-12-28 kinaba: if( i == S.size() ) 1c60f8bc27 2011-12-28 kinaba: return abs(dif); 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: LL total = rec(S, i+1, dif+(S[i]=='4')-(S[i]=='7')); 1c60f8bc27 2011-12-28 kinaba: for(char c='0'; c<S[i]; ++c) 1c60f8bc27 2011-12-28 kinaba: total += count_all(S.size() - (i+1), dif+(c=='4')-(c=='7')); 1c60f8bc27 2011-12-28 kinaba: return total; 1c60f8bc27 2011-12-28 kinaba: } 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: map<pair<int,int>, LL> memo; 1c60f8bc27 2011-12-28 kinaba: LL count_all(int n, int dif) 1c60f8bc27 2011-12-28 kinaba: { 1c60f8bc27 2011-12-28 kinaba: if( n == 0 ) 1c60f8bc27 2011-12-28 kinaba: return abs(dif); 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: const pair<int,int> key(n, dif); 1c60f8bc27 2011-12-28 kinaba: if( memo.count(key) ) 1c60f8bc27 2011-12-28 kinaba: return memo[key]; 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: LL total = 0; 1c60f8bc27 2011-12-28 kinaba: for(char c='0'; c<='9'; ++c) 1c60f8bc27 2011-12-28 kinaba: total += count_all(n-1, dif+(c=='4')-(c=='7')); 1c60f8bc27 2011-12-28 kinaba: return memo[key] = total; 1c60f8bc27 2011-12-28 kinaba: } 1c60f8bc27 2011-12-28 kinaba: }; 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: // BEGIN CUT HERE 1c60f8bc27 2011-12-28 kinaba: #include <ctime> 1c60f8bc27 2011-12-28 kinaba: double start_time; string timer() 1c60f8bc27 2011-12-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 1c60f8bc27 2011-12-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 1c60f8bc27 2011-12-28 kinaba: { os << "{ "; 1c60f8bc27 2011-12-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 1c60f8bc27 2011-12-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 1c60f8bc27 2011-12-28 kinaba: void verify_case(const long long& Expected, const long long& Received) { 1c60f8bc27 2011-12-28 kinaba: bool ok = (Expected == Received); 1c60f8bc27 2011-12-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 1c60f8bc27 2011-12-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 1c60f8bc27 2011-12-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 1c60f8bc27 2011-12-28 kinaba: #define END verify_case(_, SumOfLuckiness().theSum(A, B));} 1c60f8bc27 2011-12-28 kinaba: int main(){ 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: CASE(0) 1c60f8bc27 2011-12-28 kinaba: int A = 1; 1c60f8bc27 2011-12-28 kinaba: int B = 10; 1c60f8bc27 2011-12-28 kinaba: long long _ = 2LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(1) 1c60f8bc27 2011-12-28 kinaba: int A = 40; 1c60f8bc27 2011-12-28 kinaba: int B = 47; 1c60f8bc27 2011-12-28 kinaba: long long _ = 8LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(2) 1c60f8bc27 2011-12-28 kinaba: int A = 58; 1c60f8bc27 2011-12-28 kinaba: int B = 526; 1c60f8bc27 2011-12-28 kinaba: long long _ = 231LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(3) 1c60f8bc27 2011-12-28 kinaba: int A = 4444; 1c60f8bc27 2011-12-28 kinaba: int B = 7777; 1c60f8bc27 2011-12-28 kinaba: long long _ = 2338LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(4) 1c60f8bc27 2011-12-28 kinaba: int A = 585858585; 1c60f8bc27 2011-12-28 kinaba: int B = 858585858; 1c60f8bc27 2011-12-28 kinaba: long long _ = 287481025LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(5) 1c60f8bc27 2011-12-28 kinaba: int A = 1; 1c60f8bc27 2011-12-28 kinaba: int B = 1; 1c60f8bc27 2011-12-28 kinaba: long long _ = 0LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: CASE(6) 1c60f8bc27 2011-12-28 kinaba: int A = 1; 1c60f8bc27 2011-12-28 kinaba: int B = 2000000000; 1c60f8bc27 2011-12-28 kinaba: long long _ = -1LL; 1c60f8bc27 2011-12-28 kinaba: END 1c60f8bc27 2011-12-28 kinaba: 1c60f8bc27 2011-12-28 kinaba: } 1c60f8bc27 2011-12-28 kinaba: // END CUT HERE