0fce772883 2015-11-18 kinaba: #include <iostream> 0fce772883 2015-11-18 kinaba: #include <sstream> 0fce772883 2015-11-18 kinaba: #include <iomanip> 0fce772883 2015-11-18 kinaba: #include <vector> 0fce772883 2015-11-18 kinaba: #include <string> 0fce772883 2015-11-18 kinaba: #include <map> 0fce772883 2015-11-18 kinaba: #include <set> 0fce772883 2015-11-18 kinaba: #include <algorithm> 0fce772883 2015-11-18 kinaba: #include <numeric> 0fce772883 2015-11-18 kinaba: #include <iterator> 0fce772883 2015-11-18 kinaba: #include <functional> 0fce772883 2015-11-18 kinaba: #include <complex> 0fce772883 2015-11-18 kinaba: #include <queue> 0fce772883 2015-11-18 kinaba: #include <stack> 0fce772883 2015-11-18 kinaba: #include <cmath> 0fce772883 2015-11-18 kinaba: #include <cassert> 0fce772883 2015-11-18 kinaba: #include <tuple> 0fce772883 2015-11-18 kinaba: using namespace std; 0fce772883 2015-11-18 kinaba: typedef long long LL; 0fce772883 2015-11-18 kinaba: typedef complex<double> CMP; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: class Procrastination { public: 0fce772883 2015-11-18 kinaba: long long findFinalAssignee(long long n) 0fce772883 2015-11-18 kinaba: { 0fce772883 2015-11-18 kinaba: map<LL, set<LL>> divs; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: for(LL k=2; k*2<=n; ) { 0fce772883 2015-11-18 kinaba: if(n%k==0) ++n; 0fce772883 2015-11-18 kinaba: else if(n%k==1) --n; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: set<LL>& dn = divs[n]; 0fce772883 2015-11-18 kinaba: if(dn.empty()) 0fce772883 2015-11-18 kinaba: for(LL p=1; p*p<=n; ++p) 0fce772883 2015-11-18 kinaba: if(n%p==0) { 0fce772883 2015-11-18 kinaba: dn.insert(p); 0fce772883 2015-11-18 kinaba: dn.insert(n/p); 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: else if((n-1)%p==0) { 0fce772883 2015-11-18 kinaba: dn.insert(p); 0fce772883 2015-11-18 kinaba: dn.insert((n-1)/p); 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: k = *dn.lower_bound(k+1); 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: return n; 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: }; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: // BEGIN CUT HERE 0fce772883 2015-11-18 kinaba: #include <ctime> 0fce772883 2015-11-18 kinaba: double start_time; string timer() 0fce772883 2015-11-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0fce772883 2015-11-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0fce772883 2015-11-18 kinaba: { os << "{ "; 0fce772883 2015-11-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0fce772883 2015-11-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0fce772883 2015-11-18 kinaba: void verify_case(const long long& Expected, const long long& Received) { 0fce772883 2015-11-18 kinaba: bool ok = (Expected == Received); 0fce772883 2015-11-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0fce772883 2015-11-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0fce772883 2015-11-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0fce772883 2015-11-18 kinaba: #define END verify_case(_, Procrastination().findFinalAssignee(n));} 0fce772883 2015-11-18 kinaba: int main(){ 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: CASE(0) 0fce772883 2015-11-18 kinaba: long long n = 3LL; 0fce772883 2015-11-18 kinaba: long long _ = 3LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(1) 0fce772883 2015-11-18 kinaba: long long n = 8LL; 0fce772883 2015-11-18 kinaba: long long _ = 11LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(2) 0fce772883 2015-11-18 kinaba: long long n = 20LL; 0fce772883 2015-11-18 kinaba: long long _ = 20LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(3) 0fce772883 2015-11-18 kinaba: long long n = 196248LL; 0fce772883 2015-11-18 kinaba: long long _ = 196259LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(4) 0fce772883 2015-11-18 kinaba: long long n = 5587021440LL; 0fce772883 2015-11-18 kinaba: long long _ = 5587021440LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: /* 0fce772883 2015-11-18 kinaba: CASE(5) 0fce772883 2015-11-18 kinaba: long long n = LL; 0fce772883 2015-11-18 kinaba: long long _ = LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(6) 0fce772883 2015-11-18 kinaba: long long n = LL; 0fce772883 2015-11-18 kinaba: long long _ = LL; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: */ 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: // END CUT HERE