Advertisement
paulomiranda98

Untitled

Feb 7th, 2021
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef unsigned long long ull;
  5. ull fastPow(ull base, ull exp, ull mod){
  6.   base %= mod;
  7.   //exp %= phi(mod) if base and mod are relatively prime
  8.   ull ans = 1LL;
  9.   while (exp > 0){
  10.     if (exp & 1LL)
  11.       ans = (ans * (__int128_t)base) % mod;
  12.     base = (base * (__int128_t)base) % mod;
  13.     exp >>= 1;
  14.   }
  15.   return ans;
  16. }
  17. bool checkComposite(ull n, ull a, ull d, int s){
  18.   ull x = fastPow(a, d, n);
  19.   if (x == 1 or x == n - 1)
  20.     return false;
  21.   for (int r = 1; r < s; r++){
  22.     x = (x * (__int128_t)x) % n;
  23.     if (x == n - 1LL)
  24.       return false;
  25.   }
  26.   return true;
  27. };
  28. bool millerRabin(ull n){
  29.   if (n < 2)
  30.     return false;
  31.   int r = 0;
  32.   ull d = n - 1LL;
  33.   while ((d & 1LL) == 0){
  34.     d >>= 1;
  35.     r++;
  36.   }
  37.   for (ull a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}){
  38.     if (n == a)
  39.       return true;
  40.     if (checkComposite(n, a, d, r))
  41.       return false;
  42.   }
  43.   return true;
  44. }
  45. ull modMul(ull a, ull b, ull mod){
  46.     return (a*(__uint128_t)b)%mod;
  47. }
  48. ull f(ull x, ull mod){
  49.     return (modMul(x, x, mod) + 1)%mod;
  50. }
  51. ull pollard(ull n) {
  52.     auto f = [n](ull x){ return modMul(x, x, n) + 1; };
  53.     ull x = 0, y = 0, t = 0, prd = 2, i = 1, q;
  54.     while (t++ % 40 || __gcd(prd, n) == 1) {
  55.         if (x == y) x = ++i, y = f(x);
  56.         if ((q = modMul(prd, max(x,y) - min(x,y), n))) prd = q;
  57.         x = f(x), y = f(f(y));
  58.     }
  59.     return __gcd(prd, n);
  60. }
  61. vector<ull> factor(ull n) {
  62.     if (n == 1) return {};
  63.     if (millerRabin(n)) return {n};
  64.     ull x = pollard(n);
  65.     auto l = factor(x), r = factor(n / x);
  66.     l.insert(l.end(), r.begin(), r.end());
  67.     return l;
  68. }
  69.  
  70. int main() {
  71.     ios_base::sync_with_stdio(false); cin.tie(NULL);
  72.     ull n;
  73.     while(true){
  74.         cin >> n;
  75.         if(n == 0)
  76.             break;
  77.         auto v = factor(n);
  78.         map<ull, int> mp;
  79.         for(ull x: v)
  80.             mp[x]++;
  81.         bool can = false;
  82.         for(auto p : mp){
  83.             if(can)
  84.                 cout << " ";
  85.             cout << p.first << "^" << p.second;
  86.             can = true;
  87.         }
  88.         cout << endl;              
  89.     }
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement