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

来源:互联网 发布:mac air哪个键是insert 编辑:程序博客网 时间:2024/06/11 16:45
G - Cube Number
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu
SubmitStatusPracticeSDUT 3257
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;  }


0 0
原创粉丝点击