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
题意:给定n个数,问有几对数对能相乘得到一个平方数。。。
思路:首先我们利用唯一分解定理,给每个数降“平方”,去除每个数里的平方数,那么跟自己一样的数字才能构成平方数,那么就把个数大于2的选出两个来就好啦。。。
代码:
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
int t, n, cas, tot = 0;
const int maxn = 1e5+7;
ll sum = 0, a[maxn], k, c, p[maxn];
bool prime[maxn];
map<ll, ll> mp;
map<ll, ll> :: iterator it;
void init()
{
for(int i = 2; i < 1010; i++) prime[i] = true;
for(ll i = 2; i < 1010; i++)
{
if(prime[i]) p[tot++] = i;
for(int j = 0; j < tot&&i*p[j] < maxn; j++)
{
prime[i*p[j]] = false;
if(i%p[j] == 0) break;
}
}
}
ll FIND(ll x) {
ll ans = 1;
for(int i = 0; i < tot&& x > 1&& p[i]*p[i] <= x; i++)
{
if(x%p[i] == 0){
int c = 0;
while(x%p[i] == 0) {
x /= p[i];
c++;
}
if(c%2 != 0) ans *= p[i];
}
}
if(x > 1) ans *= x;
return ans;
}
int main()
{
init();
scanf("%d",&t);
while(t--)
{
mp.clear();
cas = 0;
sum = 0;
scanf("%d",&n);
while(n--)
{
scanf("%lld",&k);
k = FIND(k);
if(mp.count(k) == 0) mp[k] = 1;
else mp[k]++;
}
for(it = mp.begin(); it != mp.end(); it++)
if((it->second) >= 2) sum += ((it->second)- 1)*(it->second)/2;
printf("%lld\n", sum);
}
}