第六届“浪潮杯”山东省ACM H Square Number

Square Number

Time Limit: 1000 ms  Memory Limit: 65536 KiB
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大学生程序设计竞赛

题意:给定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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值