Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.Math;
  3.  
  4. public class Right {
  5. public static void solution0(int d[]) {
  6. int n = d.length;
  7. int count = 0;
  8. for(int i = 0; i < n; ++i)
  9. for(int j = i + 1; j < n; ++j) {
  10. int x = d[i] * d[i] + d[j] * d[j];
  11. for(int k = 0; k < n; ++k)
  12. if (d[k] * d[k] == x) count++;
  13. }
  14.  
  15. System.out.println(count);
  16. }
  17.  
  18. public static void solution1(int d[]) {
  19. boolean mark[] = new boolean[30000 + 1];
  20. int n = d.length;
  21. for(int i = 0; i < n; ++i) mark[ d[i] ] = true;
  22.  
  23. int count = 0;
  24. for(int i = 0; i < n; ++i)
  25. for(int j = i + 1; j < n; ++j) {
  26. int x = d[i] * d[i] + d[j] * d[j];
  27. int y = (int)(Math.sqrt(x));
  28. if ((y <= 30000) && (y * y == x) && (mark[y] == true)) {
  29. count += 1;
  30. }
  31. }
  32.  
  33. System.out.println(count);
  34. }
  35.  
  36. public static void sort(int[] a) {
  37. for(int i = 1; i < a.length; ++i) {
  38. for(int j = i - 1; j >= 0; --j)
  39. if (a[j] > a[j + 1]) {
  40. int swap = a[j];
  41. a[j] = a[j + 1];
  42. a[j + 1] = swap;
  43. }
  44. }
  45. }
  46.  
  47. public static boolean binary_search(int a[], int value) {
  48. int low = 0, high = a.length - 1;
  49. while (low <= high) {
  50. int mid = (low + high) / 2;
  51. if (a[mid] < value) low = mid + 1;
  52. else if (a[mid] > value) high = mid - 1;
  53. else return true; // a[mid] = value
  54. }
  55. return false;
  56. }
  57.  
  58. public static void solution2(int d[]) {
  59. int n = d.length;
  60. int d2[] = new int[n];
  61. for(int i = 0; i < n; ++i) d2[i] = d[i] * d[i];
  62. sort(d2);
  63.  
  64. int count = 0;
  65. for(int i = 0; i < n; ++i)
  66. for(int j = i + 1; j < n; ++j) {
  67. int x = d[i] * d[i] + d[j] * d[j];
  68. if (binary_search(d2, x) == true) {
  69. count++;
  70. }
  71. }
  72. System.out.println(count);
  73. }
  74.  
  75. public static void solution3(int d[]) {
  76. int n = d.length;
  77. sort(d);
  78.  
  79. int count = 0;
  80. for(int i = 0; i < n; ++i) {
  81. int k = i + 2;
  82. for(int j = i + 1; j < n; ++j) {
  83. int x = d[i] * d[i] + d[j] * d[j];
  84. while ((k < n) && (d[k] * d[k] < x)) k++;
  85.  
  86. if ((k < n) && (d[k] * d[k] == x))
  87. count++;
  88. }
  89. }
  90. System.out.println(count);
  91. }
  92.  
  93.  
  94. public static void main(String arg[]) {
  95. Scanner in = new Scanner(System.in);
  96. int n = in.nextInt();
  97. int d[] = new int[n];
  98.  
  99. for(int i = 0; i < n; ++i) d[i] = in.nextInt();
  100.  
  101. solution2(d);
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement