Square Number
Problem 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
Sample Output
2
Hint
Source
“浪潮杯”山东省第六届ACM大学生程序设计竞赛
题解:
性质1:对于平方数,只有平方数*平方数 = 平方数
性质2:对于非平方数,将非平方数的中平方因子除去后得到b,只有b*b才能得到平方数
求下组合数即可
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
bool isprime[1100];
vector<int> primes;
void mk_Prime() {
memset(isprime,true,sizeof(isprime));
isprime[0] = isprime[1] = 0;
for(int i=2;i<1100;i++) {
if(isprime[i]) primes.push_back(i);
for(int j=0;j<(int)primes.size() && i*primes[j] < 1100;i++)
{
isprime[i*primes[j]] = 1;
if(i % primes[j] == 0) break;
}
}
}
int vis[maxn];
int get_Factor(int x) {
int ans = 1;
for(int i = 0; i < (int)primes.size() && primes[i] * primes[i] <= x; i++)
{
int cnt = 0;
while( x % primes[i] == 0) {
x /= primes[i];
cnt++;
}
if(cnt & 1) ans *= primes[i];
}
if(x > 1) ans *= x;
return ans;
}
int main()
{
mk_Prime();
int caset;scanf("%d",&caset);
while(caset--) {
memset(vis,0,sizeof(vis));
int n;scanf("%d",&n);
for(int i=0,x;i<n;i++) {
scanf("%d",&x);
vis[get_Factor(x)]++;
}
ll ans = 0;
for(int i=1;i<maxn;i++) if(vis[i] >= 2){
ans += (ll)(vis[i])*(vis[i]-1);
}
printf("%lld\n",ans/2);
}
return 0;
}