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