ZCMU—H
来源:互联网 发布:mac键盘使用技巧 编辑:程序博客网 时间:2024/06/11 17:50
Description
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.
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 12
Sample Output
2
【分析】
题意:给定一串数字,求有多少对i,j满足a[i]*a[j]是一个平方数
经典题平方数....
首先,先对每个数进行质因数分解,质因数分解需要预处理一下素数,1000以内的素数所以直接暴力筛就行了
如果a[i]和a[j]满足条件,那么他们中某个质因数的个数和加起来一定是偶数,为了方便计算所以对每个数去掉它因子中已经成对的因子,因为自身的平方因子是无用的。
然后对剩下的那个数,在1-i中查找有没有能够组成偶数因子的数,其实就是找自己..在1-i中找自己就行了
【代码】
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int len=0;int vis[1001000]={0};int prime[10000]={0};void init(){ for(int i=2;i<1010;i++) if(!vis[i]){ prime[len++]=i;for(int j=i+i;j<1010;j+=i) vis[j]=1; }}int main(){ init(); int pp,x;scanf("%d",&pp); while(pp--) { memset(vis,0,sizeof(vis)); int n;scanf("%d",&n); int ans=0;while(n--) { scanf("%d",&x); for(int i=0;i<len;i++) { int t=prime[i]*prime[i]; if (t>x) break; while(x%t==0) x/=t; }ans+=vis[x];vis[x]++; } printf("%d\n",ans); } return 0;}
- ZCMU—H
- ZCMU—1678
- ZCMU—1605
- ZCMU—1067
- ZCMU—1607
- ZCMU—1069
- ZCMU—1611
- ZCMU—1606
- ZCMU—1774
- ZCMU—1766
- ZCMU—1752
- ZCMU—1724
- ZCMU—1623
- ZCMU—1689
- ZCMU—1667
- ZCMU—1639
- ZCMU—1668
- ZCMU—1540
- 库函数
- 快速排序和归并排序
- 安迪的第一个字典uva10815
- javaScript---oop
- Echarts折柱图异步加载
- ZCMU—H
- struts2
- Python 3.5 + PyInstaller 3.2.1打包exe出错,TypeError: read() takes no keyword arguments
- 解决ScrollView里嵌套 RecyclerVIew 时,RecyclerVIew 滑动不流畅的问题
- 利用js获取客户端ip的方法
- Centos 7 安装 OpenResty api 网关 Orange
- 3.3java面试笔试题
- Install Oracle Database 12cR2 On Oracle Enterprise Linux7.3
- eclipse导入jar包