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的题,可是到最后没有A出来,补题时,问的别人,但是没有过,当初他们的代阿在山科的机子上过了,这个题的大概思路是,先建一个数组存下平方的值,并且这个值中也没有平方的因子,这样就省了好多的空间,然后建一个数组存下把输入的数去掉平方因子后得到的数字出现了几次,每来一个数,我首先把他的平方因子拿去,然后,我用计数变量去加这个数出现了几次。最后把这个数输出,还有一个小技巧,就是在外边打表,打完后直接在程序里赋值就行啊
首先,来个山科过得代阿
#include<iostream> #include<cstdio> #include<bits/stdc++.h> using namespace std; int p[100010]; int s[1000010]; long long c(int n) { return n*(n-1)/2; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t,i,j,n,u; while(~scanf("%d",&t)) { while(t--) { memset(s,0,sizeof(s)); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&p[i]); for(j=2;j*j<=p[i];j++) { u=j*j; if(p[i]%u==0) { while(p[i]%u==0) { p[i]/=u; } } } s[p[i]]++; } sort(p,p+n); long long sum=0; for(i=0;i<n { sum+=c(s[p[i]]); i+=s[p[i]]; } printf("%lld\n",sum); } } return 0; }
然后这个是后来的,我们学校的OJ性能不好啊QAQ
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> #define LL long long using namespace std; int pm[1111]={4, 9, 25, 49, 121, 169, 289, 361, 529, 841, 961, 1369, 1681, 1849, 2209, 2809, 3481, 3721, 4489, 5041, 5329, 6241, 6889, 7921, 9409, 10201, 10609, 11449, 11881, 12769, 16129, 17161, 18769, 19321, 22201, 22801, 24649, 26569, 27889, 29929, 32041, 32761, 36481, 37249, 38809, 39601, 44521, 49729, 51529, 52441, 54289, 57121, 58081, 63001, 66049, 69169, 72361, 73441, 76729, 78961, 80089, 85849, 94249, 96721, 97969, 100489, 109561, 113569, 120409, 121801, 124609, 128881, 134689, 139129, 143641, 146689, 151321, 157609, 160801, 167281, 175561, 177241, 185761, 187489, 192721, 196249, 201601, 208849, 212521, 214369, 218089, 229441, 237169, 241081, 249001, 253009, 259081, 271441, 273529, 292681, 299209, 310249, 316969, 323761, 326041, 332929, 344569, 351649, 358801, 361201, 368449, 375769, 380689, 383161, 398161, 410881, 413449, 418609, 426409, 434281, 436921, 452929, 458329, 466489, 477481, 491401, 502681, 516961, 528529, 537289, 546121, 552049, 564001, 573049, 579121, 591361, 597529, 619369, 635209, 654481, 657721, 674041, 677329, 683929, 687241, 703921, 727609, 734449, 737881, 744769, 769129, 776161, 779689, 786769, 822649, 829921, 844561, 863041, 877969, 885481, 896809, 908209, 935089, 942841, 954529, 966289, 982081, 994009, 1018081, }; int p[1111111]; int main() { int i; int t,n,sum,x; scanf("%d",&t); while(t--) { sum = 0; memset(p,0,sizeof(p)); scanf("%d",&n); while(n--) { scanf("%d",&x); for(i = 0; pm[i] <= x; ++i) { while(!(x%pm[i])) { x /= pm[i]; } } sum += p[x]; p[x]++; } printf("%d\n",sum); } return 0; }
5702

被折叠的 条评论
为什么被折叠?



