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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class ExtendedHappyNumbers 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: long long calcTheSum(int A, int B, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> happy(9*9*9*9*9*9*7); 4fd800b3a8 2011-02-23 kinaba: // map<int,int> happy; 4fd800b3a8 2011-02-23 kinaba: LL sum = 0; 4fd800b3a8 2011-02-23 kinaba: for(int n=A; n<=B; ++n) 4fd800b3a8 2011-02-23 kinaba: sum += rec(n, K, happy); 4fd800b3a8 2011-02-23 kinaba: return sum; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int S(int K, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int sum = 0; 4fd800b3a8 2011-02-23 kinaba: for(int d=1; d<=n; d*=10) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int v = n/d%10; 4fd800b3a8 2011-02-23 kinaba: int t = 1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<K; ++i) 4fd800b3a8 2011-02-23 kinaba: t *= v; 4fd800b3a8 2011-02-23 kinaba: sum += t; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return sum; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int rec(int n, int K, vector<int>& happy) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int LOOP = -1; 4fd800b3a8 2011-02-23 kinaba: stack<int> vs; 4fd800b3a8 2011-02-23 kinaba: int m = 0x7fffffff; 4fd800b3a8 2011-02-23 kinaba: for(;;) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( happy[n] == LOOP ) 4fd800b3a8 2011-02-23 kinaba: {m = looped(n, K); break;} 4fd800b3a8 2011-02-23 kinaba: if( happy[n] ) 4fd800b3a8 2011-02-23 kinaba: {m = happy[n]; break;} 4fd800b3a8 2011-02-23 kinaba: happy[n] = LOOP; 4fd800b3a8 2011-02-23 kinaba: vs.push(n); 4fd800b3a8 2011-02-23 kinaba: n = S(K, n); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: while(!vs.empty()) { 4fd800b3a8 2011-02-23 kinaba: int v = vs.top(); vs.pop(); 4fd800b3a8 2011-02-23 kinaba: happy[v] = m = min(m, v); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return m; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int looped(int n, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int mn = n; 4fd800b3a8 2011-02-23 kinaba: vector<int> vs(1, n); 4fd800b3a8 2011-02-23 kinaba: for(int v=S(K,n); v!=n; v=S(K,v)) 4fd800b3a8 2011-02-23 kinaba: mn = min(mn, v); 4fd800b3a8 2011-02-23 kinaba: return mn; 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 long long &Expected, const long long &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 A = 13; 4fd800b3a8 2011-02-23 kinaba: int B = 13; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 1LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 5; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 14LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int A = 10; 4fd800b3a8 2011-02-23 kinaba: int B = 99; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 450LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int A = 535; 4fd800b3a8 2011-02-23 kinaba: int B = 538; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 820LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int A = 100000; 4fd800b3a8 2011-02-23 kinaba: int B = 400000; 4fd800b3a8 2011-02-23 kinaba: int K = 6; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 5169721292LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } 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: