D-- Square Pair
题目大意:
给你一个有非负整数组成长度为n的序列A,求有多少个序列对(i,j)满足:
1 <= i < j <= n, A[i] * A[j] 是一个平方数。
思路解析:
假设 A[i] * A[j] 是一个平方数 a *a, 那么他一个能表示为 a1 * a1 * a2 * a2 * ....
即A[i] 能被表示 为 g * 平方数1, A[j] 能被表示为 g * 平方数2,此情况下他们相乘才能组成一个平方数。
代码实现:
import java.io.*;
import java.util.*;
public class Main {
static long inf = 4 * (long) 1e10;
static int mod = (int) 1e9 + 7;
static int mod9 = 998244353;
public static void main(String[] args) throws IOException {
long[] pow = new long[200001];
int n = input.nextInt();
long ans = 0;
for (int i = 0; i < n; i++) {
int x = input.nextInt();
int pw = 2;
while ((pw * pw) <= x){
while (x % (pw * pw) == 0){
x /= pw * pw;
}
pw ++;
}
ans += pow[x];
pow[x]++;
}
ans += pow[0] * (n - pow[0]);
out.println(ans);
out.flush();
out.close();
br.close();
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Input input = new Input(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static class Input {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Input(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}