山东省第四届ACM省赛题——Square Number(平方数的性质)
来源:互联网 发布:js统计访问量 编辑:程序博客网 时间:2024/05/22 16:57
题目描述
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
题意不难理解,主要是数据范围很大,暴力打表肯定不行。对于平方数n,它有一个性质是n一定等于奇数个质数相乘。比如36,它可以表示为2x18,3x12,4x9,其本质都是2x2x3x3。
所以对于题目中输入的每个数,要找出它的奇数的质数,比如3和12,两个数都有一个奇数的3,所以他们能相乘得到平方数,如果有3个以上奇个数的质数,取出任意两个都能组成平方数,因此这里要用到组合公式
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <cstdio>#include <set>#include <vector>#include <iomanip>#include <stack>#include <map>#define MAXN 1000005#define mod 9973#define inf 0x3f3f3f3fusing namespace std;int p[MAXN],a[MAXN],sq[MAXN],cnt; //p保存素数,a判断当前数是否为素数,sq保存质数的平方数int vis[MAXN]; //vis保存奇个数的质数void Primer(){ int i,j, k; for(i=2;i<MAXN;++i) //i从2开始遍历 { if(a[i]==0) //a[i]==0说明该i是素数 p[cnt++]=i; //cnt作为地址标志 for(j=0;j<i&&(k=i*p[j])<MAXN;++j) //j从0开始遍历,k作为有质数p[j]的合数 { a[k]=1; //合数设1 if(i%p[j]==0) break; } }}int sum(int x){ return x*(x-1)/2; //组合公式的化简}int main(){ ios::sync_with_stdio(false); cnt=0; Primer(); int t,n,top=0,x; for(int i=0;i<cnt;++i) { sq[top++]=p[i]*p[i]; } cin>>t; while(t--) { memset(vis,0,sizeof(vis)); cin>>n; for(int i=0;i<n;++i) { cin>>x; for(int j=0;sq[j]<=x;++j) { while(x%sq[j]==0) x/=sq[j]; } vis[x]++; } int ans=0; for(int i=0;i<MAXN;++i) { ans+=sum(vis[i]); } cout<<ans<<endl; } return 0;}
- 山东省第四届ACM省赛题——Square Number(平方数的性质)
- 山东省第六届ACM大学生程序设计竞赛-Square Number(完全平方数)
- 【第七届山东省ACM竞赛】Square Number
- SDUT 3258 山东省第六届ACM程序设计大赛——H Square Number
- Square Number(完全平方数)
- 山东省第一届ACM省赛题——Greatest Number(不用dp的二分查找)
- 山东省第六届ACM竞赛 山东理工SDUT3258 Square Number
- 山东省第一届ACM省赛题——Phone Number
- 山东省ACM竞赛(2015)---H - Square Number
- 山东省第四届ACM省赛题——Rescue The Princess(计算几何)
- 山东省第四届ACM省赛题——Contest Print Server(模拟)
- 山东省第四届ACM省赛题——Alice and Bob(二进制)
- 山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
- 第四届 山东省ACM A-Number and B-Number(数位DP+二分 待整理)
- 2013山东省第四届ACM省赛 The number of steps
- 完全平方数的性质
- 山东省第四届ACM大学生程序设计竞赛 The number of steps(概率dp&全期望)
- “浪潮杯”山东省第6届acm省赛 sdut3258 Square Number sdut 3257 Cube Number
- 块元素设置inline属性不能设置宽高的解决办法
- 为整个window添加背景遮罩
- Android MarginLeft与MarginStart的区别
- matlab和C混合编程实现脉冲压缩
- 大数据分析师的卓越之道
- 山东省第四届ACM省赛题——Square Number(平方数的性质)
- java中ListView的item点击事件监听拟人理解。
- ubuntu下编写及运行C程序
- c++中的cin,cout
- 字符串计数
- VB.net学习笔记(二十一)XML流的读写
- CentOS7安装redis cluster
- MyBatis Review——加载mappers映射文件的三种方式
- LeetCode 93. Restore IP Addresses(恢复IP地址)