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: class NewCoins { public: 4fd800b3a8 2011-02-23 kinaba: int minCoins(vector <int> price) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: memo.resize(100001, -1); 4fd800b3a8 2011-02-23 kinaba: return mc(1, price); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> memo; 4fd800b3a8 2011-02-23 kinaba: int mc( int d, const vector<int>& price ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( memo[d] != -1 ) 4fd800b3a8 2011-02-23 kinaba: return memo[d]; 4fd800b3a8 2011-02-23 kinaba: if( price.size() == 0 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // all 1 4fd800b3a8 2011-02-23 kinaba: int X = accumulate(price.begin(), price.end(), 0); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int P = *max_element(price.begin(), price.end()); 4fd800b3a8 2011-02-23 kinaba: for(int p=2; p<=P; ++p) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int byOne = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> p2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<price.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: byOne += price[i] % p; 4fd800b3a8 2011-02-23 kinaba: if( memo[d*p]==-1 ) 4fd800b3a8 2011-02-23 kinaba: if(price[i] / p) p2.push_back(price[i]/p); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: X = min(X, byOne + mc(d*p, p2)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return memo[d] = X; 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(_, NewCoins().minCoins(price));} 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 price_[] = {25, 102}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 4; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {58}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {1, 4, 5, 9, 16}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 7; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {1, 1, 1, 1, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {28, 569, 587, 256, 15, 87, 927, 4, 589, 73, 98, 87, 597, 163, 6, 498}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 62; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int price_[] = {1,2,3,4,5,6,9997,9998,9999,99910,99911,99912,99913,99914,99915,99916,99917,99918,99919,99920,99921,99922,99923,99924,87625,87626,87627,87628,87629,87630,87631,87632,87633,87634,87635,87636,87637,87638,87639,99940,99991,99992,99993,99994,99995,99996,99997,99998,99999,100000}; 4fd800b3a8 2011-02-23 kinaba: vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 183; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE