山东省第六届省赛 H题:Square Number
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
题意:
平方数:某个整数的平方;
给定一串整数(a1,a2........an),求有多少对整数的乘积(ai*aj),使得这个乘积为平方数。
题解:
唯一分解定理:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积
那么平方数肯定能分解成若干素数的偶次幂,先将每个质数的平方打表,将分解后剩余的部分存入数组中,最后两两配对。
代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define is_lower(c) (c>='a' && c<='z')
#define is_upper(c) (c>='A' && c<='Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>='0' && c<='9')
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define IO ios::sync_with_stdio(0);\
cin.tie();\
cout.tie();
#define For(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
const ll inf=0x3f3f3f3f;
;
const ll inf_ll=(ll)1e18;
const ll mod=1000000007LL;
;
];
int prime[maxn],prime1[maxn];
];
];
;
void getprime()
{
memset(vis, false, sizeof(vis));
int N = sqrt(maxn);
; i <= N; ++i)
{
if ( !vis[i] )
{
prime[++num] = i;
prime1[num] = i*i;
}
; j <= num && i * prime[j] <= N ; j++)
{
vis[ i * prime[j] ] = true;
) break;
}
}
}
int main()
{
int T;
cin>>T;
getprime();
while(T--)
{
int x;
memset(cnt,,sizeof(cnt));
cin>>x;
For(i,,x)
{
int xx;
cin>>xx;
; xx>=prime1[j]&&j<=num; j++)
{
)
xx/=prime1[j];
}
cnt[xx]++;
}
ll ans = ;
For(i,,maxn-)
if(cnt[i])
ans+= cnt[i]*(cnt[i]-)/;
cout<<ans<<endl;
}
;
}
/***************************************************
User name: exist
Result: Accepted
Take time: 572ms
Take Memory: 2544KB
Submit time: 2018-03-17 13:55:51
****************************************************/
山东省第六届省赛 H题:Square Number的更多相关文章
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
Binomial Coeffcients TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 输入 输出 示例输入 1 1 10 2 9 ...
- Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)
Crack Mathmen TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take security ...
- ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...
- 山东省第六届省赛 BIGZHUGOD and His Friends II(赛瓦定理)
Description BIGZHUGOD and his three friends are playing a game in a triangle ground. The number of B ...
- 2013年省赛H题
2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- 山东省第六届ACM省赛
A.Nias and Tug-of-War(sort排序) B.Lowest Unique Price(set+map) C.Game!(博弈) D.Stars E.BIGZHUGOD and His ...
- 山东省第六届ACM省赛 H---Square Number 【思考】
题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...
随机推荐
- JAVA实现定时器功能
在接口开发时,有一种开发模式叫定时器模式,可以理解为每经过一段预设的时间就会执行一次事件,而在我们的工作中,这个事件所实现的功能一般是将两个系统的数据信息进行同步,这样就实现了两个系统通过接口进行对接 ...
- Intellij IDEA将工程打包成jar包并执行
打开File -> Project Structure -> Artifacts 点击“+”,选择“Jar”,选择Empty或From modules with dependencies, ...
- 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理
题目描述 求 $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...
- HDU——1394 Minimum Inversion Number
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- [POI2007]ATR-Tourist Attractions
题目大意:一个无向图,从$1$到$n$,要求必须经过$2,3,\dots,k+1$,给出一些限制关系,要求在经过$v\leq k+1$之前必须经过$u\leq k+1$,求最短路 题解:预处理出$1\ ...
- BZOJ1934 [Shoi2007]Vote 善意的投票 【最小割】
题目 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿 ...
- git使用笔记(八)团队协作
By francis_hao Nov 24,2016 本文由 刘英皓 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可.欢迎转载,请注明出处 ...
- 几种不同的json格式解析
转连接: http://blog.csdn.net/whx405831799/article/details/42171191 内容很好 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分 ...
- C++构造函数重载以及默认参数引起的二义性
大家都知道当我们声明一个类时,系统会提供一个默认构造函数.当我们需要提供参数进行对类数据成员进行初始化时,就需要对类的带参构造函数进行重载.同时,如果我们需要调用默认构造函数进行类数据成员的初始化时, ...
- c++ 公有继承的赋值兼容规则
赋值兼容规则是指在需要基类对象的任何地方都可以使用公有派生类的对象来替代.通过公有继承,派生类得到了基类中除构造函数.析构函数之外的所有成员,而且所有成员的访问控制属性也和基类完全相同.这样,公有派生 ...