给一张图,点数n<=100000,每个点有一个权 1≤Xi≤1000000 ,两点不互质则连边,求连通块个数。
对于每个权值把它的素因子也纳入图,然后每个权值与其素因子连边。
注意特判1
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (1000000+10)
typedef long long ll;
int p[MAXN],tot;
bool b[MAXN]={0};
int f[MAXN];
void make_prime(int n)
{
tot=0;f[1]=0;
Fork(i,2,n)
{
if (!b[i]) p[++tot]=i,f[i]=1;
For(j,tot)
{
if (i*p[j]>n) break;
b[i*p[j]]=1;f[i*p[j]]=i;
if (i%p[j]==0) break;
}
}
}
class bingchaji
{
public:
int father[MAXN],n,cnt;
void mem(int _n)
{
n=cnt=_n;
For(i,n) father[i]=i;
}
int getfather(int x)
{
if (father[x]==x) return x;
return father[x]=getfather(father[x]);
}
void unite(int x,int y)
{
// cout<<x<<" "<<y<<endl;
x=getfather(x);
y=getfather(y);
if (x^y) {
--cnt;
father[x]=y;
}
}
bool same(int x,int y)
{
return getfather(x)==getfather(y);
}
}S;
int a[MAXN];
int main() {
// freopen("G.in","r",stdin);
make_prime(1000000);
// For(i,100) cout<<i<<' '<<f[i]<<endl;
int T=read();
For(kcase,T) {
int n=read();
S.mem(1000000);
For(i,n) a[i]=read();
int ans=0;
For(i,n) if (a[i]==1) ++ans;
sort(a+1,a+1+n);
n=unique(a+1,a+1+n)-a-1;
//For(i,n) cout<<a[i]<<' ';cout<<endl;
For(i,n) if (a[i]>1) {
int t=a[i];
while(f[t]!=1) {
S.unite(a[i],t/f[t]);
t=f[t];
}
S.unite(a[i],t);
}
MEM(b)
For(i,n) if (a[i]>1)
{
int t=S.getfather(a[i]);
if (!b[t]) b[t]=1,++ans;
}
printf("Case %d: %d\n",kcase,ans);
}
return 0;
}