山东省第六届ACM大学生程序设计竞赛-Square Number(素数分解+完全平方数)

Square Number

  1000 ms         65536 KiB
Accepted/Submissions: 9/50 (18.00%)
Submit  Status    Origin

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

题意:给定一串数,求两两能够互相组合成多少个完全平方数;



思路:一个完全平方数 一定是由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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值