标签:
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
Sample Input:
1
5
1 2 3 4 12
Sample Output:
2
参考代码:
1 #include <iostream> 2 #include <stdio.h> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <cstdlib> 8 #define M 100000 + 1000 9 using namespace std; 10 int a[M] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997}; 11 int b[M]; 12 int fun(int n) 13 { 14 for(int i = 0; i < 168; i ++) //这里i一定不能大于等于168,不然可能除0;可见边界并不是越大越好: 15 { 16 while(n % (a[i] * a[i]) == 0) 17 n = n / (a[i] * a[i]); 18 if(n < (a[i] * a[i])) 19 break; 20 } 21 return n; 22 } 23 int main() 24 { 25 //freopen("in.txt","r",stdin); 26 //freopen("out.txt","w",stdout); 27 int t; 28 scanf("%d",&t); 29 while(t --) 30 { 31 int n; 32 scanf("%d",&n); 33 for(int i = 0 ; i < n; i ++) 34 { 35 scanf("%d",&b[i]); 36 b[i] = fun(b[i]); 37 } 38 sort(b,b + n); 39 long long int cnt = 0; 40 for(int i = 0; i < n; i ++) 41 { 42 long long int temp = b[i]; 43 long long int cnttemp = 0; 44 while(temp == b[i]) //第一次把i ++写进了while里面,错了,查了好久检查不出错误。唉,i ++ 不是都能写进去的; 45 { 46 cnttemp ++; 47 i ++; //改到这里就对了 48 } 49 cnt = cnt + cnttemp * (cnttemp - 1) / 2; 50 i --; 51 } 52 printf("%lld\n",cnt); 53 } 54 return 0; 55 }
山东省第六届“浪潮杯”ACM程序设计大赛:D:Square Number
标签:
原文地址:http://www.cnblogs.com/zouqihan/p/4501293.html