Square Number
1000 ms 65536 KiBAccepted/Submissions: 9/50 (18.00%)
Description
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.
Input
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).
Output
For each test case, you should output the answer of each case.
Sample
Input
1
5
1 2 3 4 12
Output
2
Source
题意:给定一串数,求两两能够互相组合成多少个完全平方数;
思路:一个完全平方数 一定是由1或者是一个数与若干素数的偶次方构成,分解每一个数,余下的记录去匹配;
代码实现:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
bool a[maxn];
int p[100005],b[maxn];
int tot,Max,ans;
void INIT(){
memset(a,0,sizeof(a));
int i,j;
tot=0;
for(i=2;i<maxn;i++){
if(!a[i]) p[tot++]=i;
for(j=0;(j<tot && i*p[j]<maxn);++j){
a[i*p[j] ]=1;
if(!(i%p[j])) break ;
}
}
// printf("%d\n",maxn);
//printf("***%d\n",tot);
}
void fun(int s)
{
int flog=0,x,y;
for(int i=0;i<tot&&s;i++)
{
x=0;
y=1;
if(s<p[i]*p[i])
break;
while(s%(p[i]*p[i])==0)
{
s/=(p[i]*p[i]);
}
}
b[s]++;
}
int main(){
INIT();
int t,n;
scanf("%d",&t);
while(t--)
{
Max=0;
scanf("%d",&n);
memset(b,0,sizeof(b));
ll x;
for(int i=0;i<n;i++)
{
scanf("%lld",&x);
fun(x);
}
ll flag=0;
//for(int i=1;i<=10;i++)
//cout<<b[i]<<endl;
for(int i=1;i<=1e6;i++)
flag+=(b[i]*(b[i]-1))/2;
printf("%d\n",flag);
}
return 0;
}