sdut3257(质因数分解)立方和数

G - Cube Number
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu
use MathJax to parse formulas

Description

In mathematics, a cube number is an integer that is the cube of an integer. In other words, it is the product of some integer with itself twice. For example, 27 is a cube number, since it can be written as 3 * 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 cube 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 9

Sample Output

2

#include<iostream>  
#include<cstring>  
#include<cstdio>  
#include<cmath>  
using namespace std;  
const int maxn  = 1000000 + 10;  
bool isprime[maxn];  
int prime[maxn];  
int num[maxn/10];  
int factor[maxn];  
int k = 0 ;  
void init(){  
    isprime[0]=1, isprime[1] =1;  
    for(int i=2;i<=1000;i++){ //只需要找1000下的素数即可  
        if(!isprime[i]){  
            prime[k++] = i;  
            for(int j = i*2;j<=1000;j+=i)  
                isprime[j] = 1;  
        }  
    }  
}  
int main(){  
    int t;  
    init();  
    scanf("%d",&t);  
    while(t--){  
        long long  ans = 0;  
        int n;  
        memset(factor,0,sizeof(factor));  
        scanf("%d",&n);  
        for(int i=0;i<n;i++)  
            scanf("%d",&num[i]);  
        for(int i=0;i<n;i++){  
            int t = num[i];  
            long long  self = 1; //代表一个数质因数分解后不是立方和数的质因数乘积的表达形式()
            long long  partner = 1; //代表要成为立方和数需要配对的最小因数 
            int isadd = 0;  
            for(int j = 0;j<k;j++){  
                int times = 0;  
                while(t % prime[j] == 0){  
                    t/=prime[j];  
                    times ++;  
                }  
                if(times % 3 == 1){  
                    self*=prime[j];  
                    partner = partner*prime[j]*prime[j];  
                }  
                else if(times % 3 == 2){  
                    self = self*prime[j]*prime[j];  
                    partner*=prime[j];  
                }  
                if(partner >1000000 ||self >1000000 ){ //如果超1000000则不可能有配对  
                    isadd = 0;break;  
                }  
                if(t == 1){  //一个数已经质因数分解完做的标记
                    isadd = 1;break;  
                }  
            }  
            if(isadd && t == 1){  
                ans += factor[partner];   //这里是如果出现的数可以与已经出现过的数匹配为立方和数那么ans+1
                factor[self]++;     //这里是出现过的数加加
            }  
        }  
        printf("%lld\n",ans);  
    }  
    return 0;  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值