sdut 3258 Square Number(山东声第六届ACM程序设计竞赛)

Square Number

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.
 

输入

 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).
 

输出

 For each test case, you should output the answer of each case.

示例输入

1   
5   
1 2 3 4 12

示例输出

2

题意:从n个数中能找出多少对平方数

首先要了解任何一个大于1的数都可以用若干个素数的乘积表示出来,对于这个题,对每一个数进行素数的累除,若是素数的偶次幂,则这个数只能和1相乘,所以找出每个数与每个素数的奇次幂,将每个数所有的与它为奇次幂的素数累乘,则就可以标记这个数;

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn = 1e6+100;

int prime[maxn];
bool iprime[maxn];
int vis[maxn];
int num;
int a[maxn];

void PrimeTable()
{
    num = 0;
    iprime[0] = true,iprime[1] = true;
    for(int i=2; i*i<maxn; i++)
    {
        if(!iprime[i])
        {
            prime[num++] = i;
            for(int j=i*2; j<=maxn; j+=i)
            {
                iprime[j] = true;
            }
        }
    }
}

int main()
{
    PrimeTable();
    int T,x,n;
    scanf("%d",&T);
    while(T-- && scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));

        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        LL ant = 0;
        for(int i=0; i<n; i++)
        {
            int ans = 1;
            for(int j=0; j<num; j++)
            {
                int Num = 0;
                while(a[i] % prime[j] == 0)
                {
                    a[i] /= prime[j];
                    Num++;
                }
                if(Num & 1)
                {
                    ans *= prime[j];
                }
                if(!iprime[a[i]] || a[i] == 1)
                {
                    ans *= a[i];
                    break;
                }
            }
            //cout<<ans<<endl;
            ant += vis[ans];
            vis[ans]++;
        }
        printf("%lld\n",ant);
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值