您的位置:首页 > 其它

sdut 3258 Square Number 打表

2016-05-20 15:21 344 查看


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



提示

 


来源

 


示例程序

 

提交 

状态 

讨论

问题是要求两两相乘为平方数

考虑到一个数可以分为若干个质数相乘的形式

如果A*B为平方数的话那么A*B可以转化为偶对个质数相乘的形式

那么我们可以把数A中偶数个质数相乘的因子去掉 如果经过相同操作后的B与之相等 那么他们就可以配对

例如 3 和 12

3 分解为 3 那么 就是 3

12 分解为 3*2*2 除去2*2 为3

那么 3和12这一对就满足条件

对于45 分解为 3*3*5 化为 5 就不能和3配对了

ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d,&x,&y,&z)
#define rdl(x) scanf("%I64d,&x);
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define ull unsigned long long
#define maxn 1000100
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define eps 1e-8
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll mul(ll a,ll b,ll p){ll sum=0;for(;b;a=(a+a)%p,b>>=1)if(b&1)sum=(sum+a)%p;return sum;}
inline void Scan(int &x) {
char c;while((c=getchar())<'0' || c>'9');x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
using namespace std;
int prime[maxn];
int dp[222];
int vis[maxn];
void init(){
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;++i){
if(!prime[i])prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
prime[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
for(int i=1;i<=200;++i)
dp[i]=prime[i]*prime[i];
}
int main(){
int n,loop,cnt=1;
init();
scanf("%d",&loop);
while(loop--){
scanf("%d",&n);
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=0;i<n;++i){
int t;
scanf("%d",&t);
for(int j=1;j<=199;++j)
if(t%dp[j]!=0)continue;
else while(t%dp[j]==0)t/=dp[j];
ans+=vis[t];
vis[t]++;
}
printf("%d\n",ans);
}
return 0;
}
/*
7
3
1 2 4
7
1 2 3 4 5 6 7
8
2 34
123 344 35 33 23 43
4
232 123 1234 33
6
1 4 9 16 25 36
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: