Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <map>
  10. #include <queue>
  11. #include <set>
  12. #include <sstream>
  13. #include <stdint.h>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17.  
  18. using namespace std;
  19.  
  20. void check_div (long long & res, long long d, long long lim)
  21. {
  22.     if (d >= lim)
  23.     {
  24.         res = min (res, d);
  25.     }
  26. }
  27.  
  28. long long min_next_div (long long n, long long lim)
  29. {
  30.     long long res = n;
  31.     for (long long d = 2; d * d <= n; d++)
  32.     {
  33.         if (n % d == 0)
  34.         {
  35.             check_div (res, d, lim);
  36.             check_div (res, n / d, lim);
  37.         }
  38.     }
  39.     return res < n ? res : 0;
  40. }
  41.  
  42. class Procrastination
  43. {
  44. public:
  45.     long long findFinalAssignee (long long n)
  46.     {
  47.         long long cur = 2;
  48.         int count = 0;
  49.         while (cur <= n)
  50.         {
  51.             long long up = min_next_div (n, cur);
  52.             long long down = min_next_div (n - 1, cur);
  53. //          printf ("%I64d %I64d\n", up, down);
  54.             count++;
  55.             if (up && (!down || up <= down))
  56.             {
  57.                 n++;
  58.                 cur = up + 1;
  59.             }
  60.             else if (down && (!up || down <= up))
  61.             {
  62.                 n--;
  63.                 cur = down + 1;
  64.             }
  65.             else
  66.             {
  67.                 break;
  68.             }
  69.         }
  70. //      return n;
  71.         return count;
  72.     }
  73. };
  74.  
  75. int main (void)
  76. {
  77.     Procrastination p;
  78.     long long n;
  79.     while (scanf (" %I64d", &n) > 0)
  80.     {
  81.         printf ("%I64d %I64d\n", n, p.findFinalAssignee (n));
  82.         fflush (stdout);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement