Advertisement
AlejandroGY

4243

May 12th, 2019
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using ll = long long;
  4. using ull = unsigned long long;
  5. constexpr int rounds = 10;
  6.  
  7. ll gcd(ll a, ll b) {
  8.     return (b == 0 ? a : gcd(b, a % b));
  9. }
  10.  
  11. ll mod_mul(ull a, ull b, ull mod) {
  12.     ull ret = 0;
  13.     for (a %= mod, b %= mod; b != 0; b >>= 1, a <<= 1, a = (a >= mod ? a - mod : a)) {
  14.         if (b & 1) {
  15.             ret += a;
  16.             if (ret >= mod) {
  17.                 ret -= mod;
  18.             }
  19.         }
  20.     }
  21.     return ret;
  22. }
  23.  
  24. ll mod_pow(ll a, ll exp, ll mod) {
  25.     ll ans = 1;
  26.     while (exp > 0) {
  27.         if(exp & 1)
  28.             ans = mod_mul(ans,a,mod);
  29.         a = mod_mul(a,a,mod);
  30.         exp >>= 1;
  31.     }
  32.     return ans;
  33. }
  34.  
  35. bool witness(ll a, ll n) {
  36.     ll u = n - 1;
  37.     ll t = 0;
  38.     if(n == a) return true;
  39.     while (u % 2 == 0) {
  40.         t++;
  41.         u >>= 1;
  42.     }
  43.  
  44.     ll next = mod_pow(a, u, n);
  45.     if (next == 1) return false;
  46.     ll last;
  47.     for (int i = 0; i < t; ++i) {
  48.         last = next;
  49.         next = mod_mul(last, last, n);
  50.         if (next == 1) {
  51.             return (last != n - 1);
  52.             }
  53.         }
  54.     return (next != 1);
  55. }
  56.  
  57. bool miller_rabin(ull n, int it = rounds) {
  58.     if (n <= 1) return false;
  59.     if (n == 2) return true;
  60.     if (n % 2 == 0) return false;
  61.  
  62.     for (int i = 0; i < it; ++i) {
  63.         ll a = std::rand( ) % (n-1) + 1;
  64.         if (witness(a, n)) {
  65.             return false;
  66.         }
  67.     }
  68.     return true;
  69. }
  70.  
  71. long long pollard_rho(ll n) {
  72.     ll x, y, i = 1, k = 2, d;
  73.     x = y = std::rand( ) % n;
  74.  
  75.     while (1) {
  76.         ++i;
  77.         x = mod_mul(x, x, n);
  78.         x += 2;
  79.        
  80.         if (x >= n) x -= n;
  81.         if (x == y) return 1;
  82.  
  83.         d = gcd(abs(x - y), n);
  84.         if (d != 1) return d;
  85.         if (i == k) {
  86.             y = x;
  87.             k *= 2;
  88.         }
  89.     }
  90.     return 1;
  91. }
  92.  
  93. std::vector<ll> factorize(ll n) {
  94.     std::vector<ll> ans;
  95.     if (n == 1) {
  96.         return ans;
  97.     }
  98.     if (miller_rabin(n)) {
  99.         ans.push_back(n);
  100.     } else {
  101.         ull d = 1ull;
  102.         while(d == 1) {
  103.             d = pollard_rho(n);
  104.         }
  105.         std::vector<ll> dd = factorize(d);
  106.         ans = factorize(n / d);
  107.         for(int i = 0; i < dd.size( ); i++) {
  108.          ans.push_back(dd[i]);
  109.       }
  110.     }
  111.     return ans;
  112. }
  113.  
  114. int main( ) {
  115.     std::ios::sync_with_stdio(0);
  116.     std::cin.tie(0);
  117.     std::cout.tie(0);
  118.  
  119.     int t;
  120.     std::cin >> t;
  121.     for (int tc = 1; tc <= t; ++tc) {
  122.         ll n;
  123.         std::cin >> n;
  124.       std::map<ll, ll> memo;
  125.         std::vector<ll> facts = factorize(n);
  126.  
  127.       for (ll i = 0; i < facts.size( ); ++i) {
  128.             if (facts[i] & 1) memo[facts[i]] += 1;
  129.         }
  130.  
  131.       long long res = 1;
  132.       for (auto act : memo) {
  133.          res *= (act.second + 1);
  134.       }
  135.       std::cout << "case " << tc << ": " << res - 1 << "\n";
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement