# “浪潮杯”山东省第6届acm省赛 sdut3258 Square Number sdut 3257 Cube Number

In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3.
Given an array of distinct integers (a1, a2, ..., an), you need to find the number of pairs (ai, aj) that satisfy (ai * aj) is a square number.

The first line of the input contains an integer T (1 ≤ T ≤ 20) which means the number of test cases.
Then T lines follow, each line starts with a number N (1 ≤ N ≤ 100000), then N integers followed (all the integers are between 1 and 1000000).

For each test case, you should output the answer of each case.

1
5
1 2 3 4 12

2

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define LL long long
#define MAX 1001000
using namespace std;
int a[MAX],flag[MAX];
bool visited[MAX];
int main()
{
int T,n,b;
int num= 0;//计数器
memset(visited,0,sizeof(visited));
int m=sqrt(MAX+0.5);//素数筛选法将MAX之内素数的平方存入数组a[]
for(int i=2;i<=m;i++)
if(!visited[i])
{
a[num++]=i*i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}

cin>>T;
while(T--)
{
memset(flag,0,sizeof(flag));
cin>>n;
for(int i=0;i<n;i++)
{
cin>>b;
for(int j=0;a[j]<=b&&j<=num-1;j++)//将b分解为若干素数的2次幂的乘积。最后剩余部分记录入flag等待匹配
{
while(b % a[j] == 0)
b /= a[j];
}
flag[b]++;
}

LL ant = 0;
for(int i=1;i<MAX;i++)
ant += flag[i]*(flag[i]-1)/2;//flag[i]表示有flag[i]个数剩余部分为i，这些数可以进行两两匹配
cout<<ant<<endl;
}
return 0;
}


In mathematics, a cube number is an integer that is the cube of an integer. In other words, it is the product of some integer with itself twice. For example, 27 is a cube number, since it can be written as 3 * 3 * 3.
Given an array of distinct integers (a1, a2, ..., an), you need to find the number of pairs (ai, aj) that satisfy (ai * aj) is a cube number.

The first line of the input contains an integer T (1 ≤ T ≤ 20) which means the number of test cases.
Then T lines follow, each line starts with a number N (1 ≤ N ≤ 100000), then N integers followed (all the integers are between 1 and 1000000).

For each test case, you should output the answer of each case.

1
5
1 2 3 4 9

2

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define MAX 1000000
#define LL long long
using namespace std;
bool visited[1000050];
int prime[100050];
int flag[1000050];
int num[100050];
int main()
{
int num=0;
int m=sqrt(MAX+0.5);
memset(visited,0,sizeof(visited));
for(int i=2;i<=m;i++)
{
if(!visited[i])
{
prime[num++]=i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}
}
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
memset(flag,0,sizeof(flag));
LL sum=0;
for(int i=0;i<=n-1;i++)
{
int m;
scanf("%d",&m);
LL a=1,b=1;
bool flagx=0;
for(int j=0;j<=num-1&&prime[j]<=m;j++)
{
LL t=prime[j]*prime[j]*prime[j];
while(m%t==0)
m=m/t;
if(m%(prime[j]*prime[j])==0)
{
m=m/(prime[j]*prime[j]);
a=a*prime[j]*prime[j];
if(!flagx)
b=b*prime[j];
}
else if(m%prime[j]==0)
{
m=m/prime[j];
a=a*prime[j];
if(!flagx)
b=b*prime[j]*prime[j];
}
if(b>MAX)
flagx=1;
}
if(!flagx)
{
b=b*m*m;
if(b<MAX)
sum+=flag[b];
}
a=a*m;
flag[a]++;
}
cout<<sum<<endl;
}
return 0;
}



筛选出<=n的素数
int num[]存筛选的素数值 ,bool visited[n];
int top = 0;
memset(visited,0,sizeof(visited));
int m=sqrt(MAX+0.5);
for(int i=2;i<=m;i++)
if(!visited[i])
{
num[top++]=i;
for(int j=i*i;j<=MAX;j+=i)
visited[j]=1;
}