Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 1356 - Prime Independence
Source : Light Online Judge
Category : Graph Theory, Number Theory
Algorithm : Maximum Bipartite Matching, Maximum Independent Set, Fopcropt Carp Algorithm
Prime Factorization
Verdict : Accepted
#include "bits/stdc++.h"
using namespace std;
#define FI freopen("in.txt", "r", stdin)
#define FO freopen("out.txt", "w", stdout)
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FOR(i, n) for (int i = 1; i <= n; i++)
#define For(i, n) for (int i = 0; i < n; i++)
#define ROF(i, n) for (int i = n; i >= 1; i--)
#define Rof(i, n) for (int i = n-1; i >= 0; i--)
#define FORI(i, n) for (auto i : n)
#define ll long long
#define ull unsigned long long
#define vi vector <int>
#define vl vector <ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define mk make_pair
#define ff first
#define ss second
#define eb emplace_back
#define em emplace
#define pb push_back
#define ppb pop_back
#define All(a) a.begin(), a.end()
#define memo(a, b) memset(a, b, sizeof a)
#define Sort(a) sort(All(a))
#define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a) reverse(All(a))
#define sz(a) (int)a.size()
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define maxAll(a) *max_element(All(a))
#define minAll(a) *min_element(All(a))
#define allUpper(a) transform(All(a), a.begin(), :: toupper)
#define allLower(a) transform(All(a), a.begin(), :: tolower)
#define endl '\n'
#define nl puts("")
#define ub upper_bound
#define lb lower_bound
#define Exp exp(1.0)
#define PIE 2*acos(0.0)
#define Sin(a) sin(((a)*PIE)/180.0)
#define EPS 1e-9
// Important Functions
// mo compare function : friend bool operator < (mo p, mo q) { int pb = p.l / block; int qb = q.l / block; if (pb != qb) return p.l < q.l; return (p.r < q.r) ^ (p.l / block % 2); }
// factorial MOD inverse : ll inv[maxn]; inline ll factorialInverse() { inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = (MOD – (MOD / i)) * inv[MOD % i) % MOD; }
/**
* uses of PBDS:
* 1) *X.find_by_order(k) : returns k'th element
* 2) X.order_of_key(k) : count total element less than k
* 3) All other operations same as set
**/
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
/**
* uses of rope data structure:
* Initialize : rope <data_type> Rope;
* Insert single element : Rope.push_back()
* Insert a block of elements : Rope.insert(position after insert, newRope)
* Erase a block of elements : Rope.erase(starting position of deletion, size of deletion)
* Other Operations : Same as std::vector
* Iterator : Rope.mutable_begin(), Rope.mutable_end(), Rope.mutable_rbegin(), Rope.mutable_rend()
* Caution : Never use -- Rope.begin() iterator
**/
#include "ext/rope"
using namespace __gnu_cxx;
// rope <int> Rope;
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
#define popcount(mask) __builtin_popcount(mask) // count set bit
#define popcountLL(mask) __builtin_popcountll(mask) // for long long
inline int read() { int a; scanf("%d", &a); return a; }
inline ll readLL() { ll a; scanf("%lld", &a); return a; }
inline double readDD() { double a; scanf("%lf", &a); return a; }
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
#define inf 1e17
#define mod 1000000007
static const int maxn = 4e4 + 5;
static const int logn = 18;
static const int mx = 500000 + 6;
vi primefactor[mx], primeList;
bool isprime[mx];
inline void primeFactorization()
{
for (int i = 2; i < mx; i++)
{
if (isprime[i]) continue;
primeList.eb(i);
for (int j = 1; j*i < mx; j++)
{
isprime[i*j] = 1;
primefactor[i*j].eb(i);
}
}
}
inline void seive()
{
memo(isprime, 1);
isprime[0] = isprime[1] = 0;
for (int i = 4; i < mx; i += 2) isprime[i] = 0;
for (int i = 3; i*i <= mx; i += 2)
{
if (isprime[i])
{
for (int j = i*i; j < mx; j += i+i)
{
isprime[j] = 0;
}
}
}
primeList.eb(2);
for (int i = 3; i < mx; i += 2) if (isprime[i]) primeList.eb(i);
}
vi graph[maxn];
int n, arr[maxn], position[mx];
inline void clean()
{
For(i, maxn) graph[i].clear();
memo(position, -1);
}
/** Hopcropt Carp Algorithm **/
#define inf 1 << 28
#define NILL 0
static const int MAXN = 4e4 + 5;
int elements_in_left_side; // no.of elements in left side
int elements_in_right_side; // no.of elements in right side
// int pairU[MAXN], pairV[MAXN], dist[MAXN];
int match[MAXN], dist[MAXN]; // if we cannot divide the graph in two parts
// use only match[]
inline void initialize_hopcroptcarp()
{
for (int i = 0; i < MAXN; i++)
{
// pairU[i] = NILL;
// pairV[i] = NILL;
match[i] = NILL;
}
}
inline bool bfs()
{
queue <int> PQ;
for (int u = 1; u <= elements_in_left_side; u++)
{
if (match[u] == NILL)
{
dist[u] = NILL;
PQ.push(u);
}
else
{
dist[u] = inf;
}
}
dist[NILL] = inf;
while (!PQ.empty())
{
int u = PQ.front(); PQ.pop();
if (u != NILL && dist[u] < dist[NILL])
{
for (int v : graph[u])
{
if (dist[ match[v] ] == inf)
{
dist[ match[v] ] = dist[u] + 1;
PQ.push(match[v]);
}
}
}
}
if (dist[NILL] != inf) return true;
else return false;
}
inline bool dfs(int u)
{
if (u != NILL)
{
for (int v : graph[u])
{
if (dist[ match[v] ] == dist[u] + 1)
{
if (dfs(match[v]))
{
match[u] = v;
match[v] = u;
return true;
}
}
}
dist[u] = inf;
return false;
}
return true;
}
inline int hopcropt_carp()
{
int max_match = 0;
while (bfs())
{
for (int u = 1; u <= elements_in_left_side; u++)
{
if (match[u] == NILL && dfs(u)) max_match++;
}
}
return max_match;
}
int main()
{
// FI;
seive();
// primeFactorization(); // this cause MLE
int tc = read();
FOR(tcase, tc)
{
clean();
n = read();
int maxnum = -1;
FOR(i, n)
{
arr[i] = read();
position[ arr[i] ] = i;
maxnum = max(maxnum, arr[i]);
}
FOR(i, n)
{
int num = arr[i];
// FORI(pf, primefactor[num])
// {
// if (position[ num/pf ] != -1)
// {
// int u = position[ num/pf ];
// int v = i;
// graph[u].eb(v);
// graph[v].eb(u);
// }
// }
for (int p : primeList)
{
int num2 = p * num;
if (num2 > maxnum) break;
if (position[num2] != -1)
{
int u = i;
int v = position[num2];
graph[u].eb(v);
graph[v].eb(u);
}
}
}
elements_in_left_side = n;
elements_in_right_side = n;
initialize_hopcroptcarp();
int maxmatch = hopcropt_carp();
int ans = n - maxmatch;
printf("Case %d: %d\n", tcase, ans);
}
}