ShajibEwuCse

https://www.spoj.com/problems/MAIN12B/

Apr 14th, 2021
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FAST             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  4. #define endl             '\n'
  5. #define loop(i,n)        for(int i=0;i<n;i++)
  6. #define CASE_PRINT       cout<<"Case "<<C<<": "
  7. #define CASE_PRINT2      cout<<"Case "<<C<<":"<<endl
  8. #define ll               long long
  9. #define ld               long double
  10. #define Pi               2*acos(0.0) // acos(-1.0)
  11. #define err              1e-9
  12. int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
  13. int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
  14.  
  15. const int mx = 1e7+123;
  16. bitset<mx> isPrime; ///if boolean array use n memory, bitset will use (n/32) memory. initially all index 0
  17. vector<int> prime; ///store all prime.
  18.  
  19. ///sieve Function..
  20. void sieve ( int n )
  21. {
  22.     n += 100;///for safety.
  23.  
  24.     isPrime[2] = 1;
  25.     prime.push_back(2);
  26.     ///all even no composite except 2. loop through only odd no.
  27.     for ( int i = 3; i <= n; i += 2 ) isPrime[i] = 1;///initially all odd are prime.
  28.  
  29.     int lim = sqrt(n*1) + 2;///for divisor
  30.  
  31.     for ( int i = 3; i <= n; i += 2 ) {
  32.         if ( isPrime[i] == 1 ) {
  33.  
  34.            prime.push_back(i);///if we get (i) it must be prime.
  35.  
  36.            if(i<=lim)///if we don't do it, (i*i) may overflow.
  37.            {
  38.                for ( int j = i*i; j <= n; j += ( i + i ) )isPrime[j] = 0;///cutting all divisor of the prime(i).
  39.            }
  40.         }
  41.     }
  42. }
  43.  
  44. ///Prime Factorization Function..
  45. vector<ll> factor (ll n)
  46. {
  47.     vector<ll> ret;
  48.  
  49.     for ( auto p : prime ) {
  50.         if ( 1LL * p * p > n ) break;
  51.  
  52.         if ( n % p == 0 ) {
  53.             ret.push_back (p); ///store just one divisor from same type multiple divisors. remove duplicate
  54.  
  55.             while ( n % p == 0 ) {
  56.                 ///ret.push_back (p);
  57.                 n /= p;
  58.             }
  59.         }
  60.     }
  61.  
  62.     if ( n > 1 ) ret.push_back(n);
  63.  
  64.     return ret;
  65. }
  66.  
  67. int main()
  68. {
  69.     FAST;
  70.     sieve(1e6); ///it'll work for (1e14).
  71.  
  72.     int t,cs = 1;cin>>t;
  73.     while(t--)
  74.     {
  75.         ll n;
  76.         cin >> n ;
  77.  
  78.  
  79.         set < ll > ans;
  80.  
  81.         for(int i=1; i<=n; i++)
  82.         {
  83.             ll num;
  84.             cin>>num;
  85.  
  86.             vector<ll>v = factor(num);
  87.  
  88.             for(auto x : v)ans.insert(x);
  89.         }
  90.  
  91.         cout<<"Case #"<<cs<<": "<<ans.size()<<endl;
  92.         for(auto i : ans)cout<<i<<endl;
  93.  
  94.  
  95.         ans.clear();///must use this function for clear factor vector.
  96.        
  97.         cs++;
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment