山東省ACMコンテスト(2015)---H-Square Number

2259 ワード

Square Number
Time Limit:1000 ms Memory limit:65536 K質問は?ここをクリック^^;
タイトルの説明
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

AC CODE:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#define AMAX 1000001
#define BMAX 1001
using namespace std;
int kPrimeBase_1000=0;
int PrimeBase_1000[BMAX];
int PrimeDisolve_list[AMAX];
int s[100001];
void prime()
{
    int i,j;
    for(j=2; j<BMAX; j++)
    {
        for(i=0; i<kPrimeBase_1000; i++)
        {
            if(j%PrimeBase_1000[i]==0)break;
        }
        if(i==kPrimeBase_1000)PrimeBase_1000[kPrimeBase_1000++]=j;
    }
}
int main()
{
    prime();
    int i,j;
    int base,ba;
    for(i=1; i<AMAX; i++)
        PrimeDisolve_list[i]=i;
    for(i=0; i<kPrimeBase_1000; i++)
    {
        base=PrimeBase_1000[i]*PrimeBase_1000[i];
        j=base;
        while(j<AMAX)
        {
            while(PrimeDisolve_list[j]%base==0)
                PrimeDisolve_list[j]/=base;
            j+=base;
        }
    }
    int T;
    int n;
    int t,time;
    long long sum;
    cin>>T;
    while(T--)
    {
        sum=0;
        multiset<int> st;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>s[i];
            t=PrimeDisolve_list[s[i]];
            time=st.count(t);
            sum+=time;
            st.insert(t);
        }
        cout<<sum<<endl;
    }
    return 0;
}