Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <ctime>
  5. using namespace std;
  6. typedef unsigned long long ull;
  7.  
  8. const ull BASE = 10000000000ULL; //upper bound for prime numbers (for original problem 100000)
  9.  
  10. inline ull gcd(ull a, ull b) {
  11.     while (b) {
  12.         a %= b;
  13.         swap(a, b);
  14.     }
  15.     return a;
  16. }
  17.  
  18. inline ull add(ull a, ull b, ull mod) {
  19.     if (a + b / 2 >= mod) return (a + b / 2 - mod) + (b - b / 2);
  20.     ull c = a + b;
  21.     return (c >= mod ? c - mod : c);
  22. }
  23.  
  24. inline ull mul(ull a, ull b, ull mod) {
  25.     ull c = 0;
  26.     while (b) {
  27.         if (b & 1) {
  28.             c = add(c, a, mod);
  29.         }
  30.         a = add(a, a, mod);
  31.         b >>= 1;
  32.     }
  33.     return c;
  34. }
  35.  
  36. inline ull powi(ull a, ull b, ull mod) {
  37.     ull c = 1;
  38.     while (b) {
  39.         if (b & 1)
  40.             c = mul(c, a, mod);
  41.         a = mul(a, a, mod);
  42.         b >>= 1;
  43.     }
  44.     return c;
  45. }
  46.  
  47. ull stupid(ull n) {
  48.     for (ull i = 2; i*i <= n; i++) {
  49.         if (n % i == 0)
  50.             return i;
  51.     }
  52.     return 1;
  53. }
  54.  
  55. inline bool miller(ull n, int s, ull t, ull a) {
  56.     ull x = powi(a, t, n);
  57.     if (x == 1 || x == n - 1) return true;
  58.     for (int i = 0; i < s - 1; i++) {
  59.         x = mul(x, x, n);
  60.         if (x == 1) return false;
  61.         if (x == n - 1) return true;
  62.     }
  63.     return false;
  64. }
  65.  
  66. inline bool miller(ull n) {
  67.     ull t = n - 1;
  68.     int s = 0;
  69.     while (~t & 1) {
  70.         s++; t >>= 1;
  71.     }
  72.     if (!miller(n, s, t, 2ULL)) return false;
  73.     if (!miller(n, s, t, 325ULL)) return false;
  74.     if (!miller(n, s, t, 9375ULL % n)) return false;
  75.     if (!miller(n, s, t, 28178ULL % n)) return false;
  76.     if (!miller(n, s, t, 450775ULL % n)) return false;
  77.     if (!miller(n, s, t, 9780504ULL % n)) return false;
  78.     if (!miller(n, s, t, 1795265022ULL % n)) return false;
  79.     return true;
  80. }
  81.  
  82. ull pollard(ull n) {
  83.     if (n < 1000)
  84.         return stupid(n);
  85.     if (miller(n))
  86.         return 1;
  87.     ull x = 997;
  88.     ull y = 1;
  89.     ull i = 0;
  90.     ull stage = 2;
  91.     while (gcd(n, (x > y ? x - y : y - x)) == 1) {
  92.         if (i == stage) {
  93.             y = x;
  94.             stage <<= 1;
  95.         }
  96.         x = add(mul(x, x, n), 1, n);
  97.         i++;
  98.     }
  99.     return gcd(n, (x > y ? x - y : y - x));
  100. }
  101.  
  102. ull reverse(ull n) {
  103.     ull p = 0;
  104.     while (n) {
  105.         p = p * 10 + n % 10;
  106.         n /= 10;
  107.     }
  108.     return p;
  109. }
  110.  
  111. void solve() {
  112.     for (ull x = BASE - 1; x; x--) {
  113.         ull n = x * (BASE / 10ULL) + reverse(x / 10ULL); //palindrome
  114.         ull a = pollard(n); //first divider
  115.         ull b = n / a; //second divider
  116.         if (a != 1 && miller(a) && miller(b) && a < BASE && b < BASE) {
  117.             cout << a << " * " << b << " = " << n << endl;
  118.             break;
  119.         }
  120.     }
  121. }
  122.  
  123. int main() {
  124.     ios::sync_with_stdio(0);
  125.  
  126.     int times = 30;
  127.     for (int i = times; i--;) {
  128.         solve();
  129.     }
  130.     cerr << clock() * 1.0 / times / CLOCKS_PER_SEC << endl;
  131.  
  132. #ifdef AndreaB330
  133.     system("pause");
  134. #endif
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement