暴力破解会超时,用因式分解的平方差公式
a^2^+ 2ab+b^2^=(a+)^2^
a²-2ab+b^2^=(a-b)^2^
输出整数=((a1+a2+a3+...+an)²-a1²-a2²-a3²-...-an²)/2
java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long mul = 0, sum = 0;
for (long i = 1; i <= n; i++) {
long x = scanner.nextLong();
sum += (x * x);
mul += x;
}
System.out.println((mul * mul - sum) / 2);
}
}