ZCMU—H

来源:互联网 发布:mac键盘使用技巧 编辑:程序博客网 时间:2024/06/11 17:50
H - Square Number
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

【分析】

题意:给定一串数字,求有多少对i,j满足a[i]*a[j]是一个平方数

经典题平方数....

首先,先对每个数进行质因数分解,质因数分解需要预处理一下素数,1000以内的素数所以直接暴力筛就行了

如果a[i]和a[j]满足条件,那么他们中某个质因数的个数和加起来一定是偶数,为了方便计算所以对每个数去掉它因子中已经成对的因子,因为自身的平方因子是无用的。
然后对剩下的那个数,在1-i中查找有没有能够组成偶数因子的数,其实就是找自己..在1-i中找自己就行了

【代码】

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int len=0;int vis[1001000]={0};int prime[10000]={0};void init(){    for(int i=2;i<1010;i++)    if(!vis[i]){        prime[len++]=i;for(int j=i+i;j<1010;j+=i) vis[j]=1;    }}int main(){    init();    int pp,x;scanf("%d",&pp);    while(pp--)    {        memset(vis,0,sizeof(vis));        int n;scanf("%d",&n);        int ans=0;while(n--)        {            scanf("%d",&x);            for(int i=0;i<len;i++)            {            int t=prime[i]*prime[i];            if (t>x) break;                while(x%t==0) x/=t;            }ans+=vis[x];vis[x]++;        }        printf("%d\n",ans);    }     return 0;}


0 0
原创粉丝点击