Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.Math;
- public class Right {
- public static void solution0(int d[]) {
- int n = d.length;
- int count = 0;
- for(int i = 0; i < n; ++i)
- for(int j = i + 1; j < n; ++j) {
- int x = d[i] * d[i] + d[j] * d[j];
- for(int k = 0; k < n; ++k)
- if (d[k] * d[k] == x) count++;
- }
- System.out.println(count);
- }
- public static void solution1(int d[]) {
- boolean mark[] = new boolean[30000 + 1];
- int n = d.length;
- for(int i = 0; i < n; ++i) mark[ d[i] ] = true;
- int count = 0;
- for(int i = 0; i < n; ++i)
- for(int j = i + 1; j < n; ++j) {
- int x = d[i] * d[i] + d[j] * d[j];
- int y = (int)(Math.sqrt(x));
- if ((y <= 30000) && (y * y == x) && (mark[y] == true)) {
- count += 1;
- }
- }
- System.out.println(count);
- }
- public static void sort(int[] a) {
- for(int i = 1; i < a.length; ++i) {
- for(int j = i - 1; j >= 0; --j)
- if (a[j] > a[j + 1]) {
- int swap = a[j];
- a[j] = a[j + 1];
- a[j + 1] = swap;
- }
- }
- }
- public static boolean binary_search(int a[], int value) {
- int low = 0, high = a.length - 1;
- while (low <= high) {
- int mid = (low + high) / 2;
- if (a[mid] < value) low = mid + 1;
- else if (a[mid] > value) high = mid - 1;
- else return true; // a[mid] = value
- }
- return false;
- }
- public static void solution2(int d[]) {
- int n = d.length;
- int d2[] = new int[n];
- for(int i = 0; i < n; ++i) d2[i] = d[i] * d[i];
- sort(d2);
- int count = 0;
- for(int i = 0; i < n; ++i)
- for(int j = i + 1; j < n; ++j) {
- int x = d[i] * d[i] + d[j] * d[j];
- if (binary_search(d2, x) == true) {
- count++;
- }
- }
- System.out.println(count);
- }
- public static void solution3(int d[]) {
- int n = d.length;
- sort(d);
- int count = 0;
- for(int i = 0; i < n; ++i) {
- int k = i + 2;
- for(int j = i + 1; j < n; ++j) {
- int x = d[i] * d[i] + d[j] * d[j];
- while ((k < n) && (d[k] * d[k] < x)) k++;
- if ((k < n) && (d[k] * d[k] == x))
- count++;
- }
- }
- System.out.println(count);
- }
- public static void main(String arg[]) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int d[] = new int[n];
- for(int i = 0; i < n; ++i) d[i] = in.nextInt();
- solution2(d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement