fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6. private static int[] st;
  7.  
  8. public static void main(String[] args)throws IOException{
  9. InputReader in = new InputReader(System.in);
  10. int n = in.nextInt();
  11. int k = in.nextInt();
  12. int[] a = new int[n+1];
  13. boolean found = false;
  14. boolean fnd = false;
  15. for(int i=1;i<=n;i++){
  16. a[i] = in.nextInt();
  17. if(a[i]%k==0){
  18. if(a[i]==k){
  19. fnd = true;
  20. }
  21. found = true;
  22. }
  23. }
  24.  
  25. if(fnd ==true){
  26. pw.println("1");
  27. }
  28.  
  29. else if(found==false){
  30. pw.println("-1");
  31. }
  32.  
  33. else{
  34. constructSegmentTree(a);
  35. int min = 1000000009;
  36. for(int i=1;i<n;i++){
  37. if(a[i]%k!=0){
  38. continue;
  39. }
  40. int low = i+1;
  41. int high = n;
  42. int mid = 0;
  43. int note = 0;
  44. while(true){
  45. mid = low + (high-low)/2;
  46. if(findRangeGcd(i, mid, a)>k){
  47. low = mid;
  48. }
  49.  
  50. else if(findRangeGcd(i, mid, a)==k){
  51. high = mid;
  52. note = mid;
  53. break;
  54. }
  55.  
  56. else{
  57. high = mid;
  58. }
  59.  
  60. if(Math.abs(high-low)<=1){
  61. if(findRangeGcd(i, low, a)==k){
  62. note = low;
  63. break;
  64. }
  65.  
  66. else if(findRangeGcd(i, high, a)==k){
  67. note = high;
  68. break;
  69. }
  70.  
  71. else{
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. if(note!=0){
  78. min = Math.min(min, note - i + 1);
  79. }
  80. }
  81. if(min == 1000000009)
  82. pw.println("-1");
  83. else
  84. pw.println(min);
  85. }
  86. pw.close();
  87. }
  88.  
  89. public static int[] constructSegmentTree(int[] arr){
  90. int height = (int)Math.ceil(Math.log(arr.length)/Math.log(2));
  91. int size = 2*(int)Math.pow(2, height)-1;
  92. st = new int[size];
  93. constructST(arr, 0, arr.length-1, 0);
  94. return st;
  95. }
  96.  
  97. public static int constructST(int[] arr, int ss, int se, int si){
  98. if(ss==se){
  99. st[si] = arr[ss];
  100. return st[si];
  101. }
  102. int mid = ss+(se-ss)/2;
  103. st[si] = gcd(constructST(arr, ss, mid, si*2+1),constructST(arr, mid+1, se, si*2+2));
  104. return st[si];
  105. }
  106.  
  107. private static int gcd(int a, int b) {
  108.  
  109. if(a<b){
  110. int c = b;
  111. b=a;
  112. a=c;
  113. }
  114. if(b==0){
  115. return a;
  116. }
  117.  
  118. if(b==1)
  119. return 1;
  120.  
  121. return gcd(b,a%b);
  122. }
  123.  
  124. //Finding The gcd of given Range
  125. public static int findRangeGcd(int ss, int se, int[] arr){
  126. int n = arr.length;
  127. if(ss<0 || se > n-1 || ss>se){
  128. throw new IllegalArgumentException("Invalid arguments");
  129. }
  130. return findGcd(0, n-1, ss, se, 0);
  131. }
  132.  
  133. public static int findGcd(int ss, int se, int qs, int qe, int si){
  134. if(ss>qe || se < qs)return 0;
  135. if(qs<=ss && qe>=se)return st[si];
  136.  
  137. int mid = ss+(se-ss)/2;
  138. return gcd(findGcd(ss, mid, qs, qe, si*2+1),findGcd(mid+1, se, qs, qe, si*2+2));
  139. }
  140.  
  141. static class InputReader {
  142.  
  143. private InputStream stream;
  144. private byte[] buf = new byte[8192];
  145. private int curChar;
  146. private int snumChars;
  147. private SpaceCharFilter filter;
  148.  
  149. public InputReader(InputStream stream) {
  150. this.stream = stream;
  151. }
  152.  
  153. public int snext() {
  154. if (snumChars == -1)
  155. throw new InputMismatchException();
  156. if (curChar >= snumChars) {
  157. curChar = 0;
  158. try {
  159. snumChars = stream.read(buf);
  160. } catch (IOException e) {
  161. throw new InputMismatchException();
  162. }
  163. if (snumChars <= 0)
  164. return -1;
  165. }
  166. return buf[curChar++];
  167. }
  168.  
  169. public int nextInt() {
  170. int c = snext();
  171. while (isSpaceChar(c))
  172. c = snext();
  173. int sgn = 1;
  174. if (c == '-') {
  175. sgn = -1;
  176. c = snext();
  177. }
  178.  
  179. int res = 0;
  180.  
  181. do {
  182. if (c < '0' || c > '9')
  183. throw new InputMismatchException();
  184. res *= 10;
  185. res += c - '0';
  186. c = snext();
  187. } while (!isSpaceChar(c));
  188.  
  189. return res * sgn;
  190. }
  191.  
  192. public long nextLong() {
  193. int c = snext();
  194. while (isSpaceChar(c))
  195. c = snext();
  196. int sgn = 1;
  197. if (c == '-') {
  198. sgn = -1;
  199. c = snext();
  200. }
  201.  
  202. long res = 0;
  203.  
  204. do {
  205. if (c < '0' || c > '9')
  206. throw new InputMismatchException();
  207. res *= 10;
  208. res += c - '0';
  209. c = snext();
  210. } while (!isSpaceChar(c));
  211.  
  212. return res * sgn;
  213. }
  214.  
  215. public String readString() {
  216. int c = snext();
  217. while (isSpaceChar(c))
  218. c = snext();
  219. StringBuilder res = new StringBuilder();
  220. do {
  221. res.appendCodePoint(c);
  222. c = snext();
  223. } while (!isSpaceChar(c));
  224. return res.toString();
  225. }
  226.  
  227. public boolean isSpaceChar(int c) {
  228. if (filter != null)
  229. return filter.isSpaceChar(c);
  230. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  231. }
  232.  
  233. public interface SpaceCharFilter {
  234. public boolean isSpaceChar(int ch);
  235. }
  236. }
  237. }
  238.  
Success #stdin #stdout 0.09s 320576KB
stdin
4 3
5 6 9 7 10
stdout
2