15635fe487 2019-07-06 kinaba: #include <iostream> 15635fe487 2019-07-06 kinaba: #include <sstream> 15635fe487 2019-07-06 kinaba: #include <iomanip> 15635fe487 2019-07-06 kinaba: #include <vector> 15635fe487 2019-07-06 kinaba: #include <string> 15635fe487 2019-07-06 kinaba: #include <map> 15635fe487 2019-07-06 kinaba: #include <set> 15635fe487 2019-07-06 kinaba: #include <algorithm> 15635fe487 2019-07-06 kinaba: #include <numeric> 15635fe487 2019-07-06 kinaba: #include <iterator> 15635fe487 2019-07-06 kinaba: #include <functional> 15635fe487 2019-07-06 kinaba: #include <complex> 15635fe487 2019-07-06 kinaba: #include <queue> 15635fe487 2019-07-06 kinaba: #include <stack> 15635fe487 2019-07-06 kinaba: #include <cmath> 15635fe487 2019-07-06 kinaba: #include <cassert> 15635fe487 2019-07-06 kinaba: #include <tuple> 15635fe487 2019-07-06 kinaba: using namespace std; 15635fe487 2019-07-06 kinaba: typedef long long LL; 15635fe487 2019-07-06 kinaba: typedef complex<double> CMP; 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: class DivisorSequences { public: 15635fe487 2019-07-06 kinaba: int longest(int N) 15635fe487 2019-07-06 kinaba: { 15635fe487 2019-07-06 kinaba: int best = 0; 15635fe487 2019-07-06 kinaba: if (N - 1 >= 2) 15635fe487 2019-07-06 kinaba: best = 1 + cnt(N - 1); 15635fe487 2019-07-06 kinaba: best = max(cnt(N), best); 15635fe487 2019-07-06 kinaba: return best; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: // The case starting with 2 or above 15635fe487 2019-07-06 kinaba: int cnt(int N) 15635fe487 2019-07-06 kinaba: { 15635fe487 2019-07-06 kinaba: int best = 1; // [N] 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: vector<int> divs; 15635fe487 2019-07-06 kinaba: for (int p = 2; p*p <= N && p < N; ++p) 15635fe487 2019-07-06 kinaba: if (N%p == 0) { 15635fe487 2019-07-06 kinaba: divs.push_back(p); 15635fe487 2019-07-06 kinaba: divs.push_back(N/p); 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: for (int s: divs) 15635fe487 2019-07-06 kinaba: if ((N-s)/s>=2) { 15635fe487 2019-07-06 kinaba: best = max(best, 1 + cnt((N - s) / s)); 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: return best; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: }; 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: // BEGIN CUT HERE 15635fe487 2019-07-06 kinaba: #include <ctime> 15635fe487 2019-07-06 kinaba: double start_time; string timer() 15635fe487 2019-07-06 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 15635fe487 2019-07-06 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 15635fe487 2019-07-06 kinaba: { os << "{ "; 15635fe487 2019-07-06 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 15635fe487 2019-07-06 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 15635fe487 2019-07-06 kinaba: void verify_case(const int& Expected, const int& Received) { 15635fe487 2019-07-06 kinaba: bool ok = (Expected == Received); 15635fe487 2019-07-06 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 15635fe487 2019-07-06 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 15635fe487 2019-07-06 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 15635fe487 2019-07-06 kinaba: #define END verify_case(_, DivisorSequences().longest(N));} 15635fe487 2019-07-06 kinaba: int main(){ 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: CASE(0) 15635fe487 2019-07-06 kinaba: int N = 15; 15635fe487 2019-07-06 kinaba: int _ = 4; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(1) 15635fe487 2019-07-06 kinaba: int N = 12; 15635fe487 2019-07-06 kinaba: int _ = 2; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(2) 15635fe487 2019-07-06 kinaba: int N = 34; 15635fe487 2019-07-06 kinaba: int _ = 4; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(3) 15635fe487 2019-07-06 kinaba: int N = 1000000000; 15635fe487 2019-07-06 kinaba: int _ = -1; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(4) 15635fe487 2019-07-06 kinaba: int N = 1 << 30; 15635fe487 2019-07-06 kinaba: int _ = -1; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(5) 15635fe487 2019-07-06 kinaba: int N = 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23; 15635fe487 2019-07-06 kinaba: int _ = -1; 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: // END CUT HERE