Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
- #define endl '\n'
- #define loop(i,n) for(int i=0;i<n;i++)
- #define CASE_PRINT cout<<"Case "<<C<<": "
- #define CASE_PRINT2 cout<<"Case "<<C<<":"<<endl
- #define ll long long
- #define ld long double
- #define Pi 2*acos(0.0) // acos(-1.0)
- #define err 1e-9
- int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
- int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
- const int mx = 1e7+123;
- bitset<mx> isPrime; ///if boolean array use n memory, bitset will use (n/32) memory. initially all index 0
- vector<int> prime; ///store all prime.
- ///sieve Function..
- void sieve ( int n )
- {
- n += 100;///for safety.
- isPrime[2] = 1;
- prime.push_back(2);
- ///all even no composite except 2. loop through only odd no.
- for ( int i = 3; i <= n; i += 2 ) isPrime[i] = 1;///initially all odd are prime.
- int lim = sqrt(n*1) + 2;///for divisor
- for ( int i = 3; i <= n; i += 2 ) {
- if ( isPrime[i] == 1 ) {
- prime.push_back(i);///if we get (i) it must be prime.
- if(i<=lim)///if we don't do it, (i*i) may overflow.
- {
- for ( int j = i*i; j <= n; j += ( i + i ) )isPrime[j] = 0;///cutting all divisor of the prime(i).
- }
- }
- }
- }
- ///Prime Factorization Function..
- vector<ll> factor (ll n)
- {
- vector<ll> ret;
- for ( auto p : prime ) {
- if ( 1LL * p * p > n ) break;
- if ( n % p == 0 ) {
- ret.push_back (p); ///store just one divisor from same type multiple divisors. remove duplicate
- while ( n % p == 0 ) {
- ///ret.push_back (p);
- n /= p;
- }
- }
- }
- if ( n > 1 ) ret.push_back(n);
- return ret;
- }
- int main()
- {
- FAST;
- sieve(1e6); ///it'll work for (1e14).
- int t,cs = 1;cin>>t;
- while(t--)
- {
- ll n;
- cin >> n ;
- set < ll > ans;
- for(int i=1; i<=n; i++)
- {
- ll num;
- cin>>num;
- vector<ll>v = factor(num);
- for(auto x : v)ans.insert(x);
- }
- cout<<"Case #"<<cs<<": "<<ans.size()<<endl;
- for(auto i : ans)cout<<i<<endl;
- ans.clear();///must use this function for clear factor vector.
- cs++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment