Advertisement
AlejandroGY

Untitled

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