fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int gcd(int a, int b)
  11. {
  12. while(b!=0)
  13. {
  14. int temp=a%b;
  15. a=b;
  16. b=temp;
  17. }
  18. return a;
  19.  
  20. }
  21.  
  22. static boolean isgcd1(List<Integer> li)
  23. {
  24. for(int i=0;i<li.size()-1;i++)
  25. {
  26. if(gcd(li.get(i),li.get(i+1))==1)
  27. return true;
  28. }
  29. return false;
  30. }
  31. public static void main (String[] args) throws java.lang.Exception
  32. {
  33. // your code goes here
  34. Scanner sc=new Scanner(System.in);
  35. int n=sc.nextInt();
  36. int a[]=new int[n];
  37. int k=sc.nextInt();
  38. for(int i=0;i<n;i++)
  39. a[i]=sc.nextInt();
  40. int count=0;
  41. // int n=a.length;
  42. for(int i=0;i<n;i++)
  43. {
  44. List<Integer> li=new ArrayList<>();
  45. for(int j=0;j<k;j++)
  46. {
  47. li.add(a[(i+j)%n]);
  48. }
  49. if(!isgcd1(li))
  50. count++;
  51. }
  52.  
  53. System.out.println(count);
  54.  
  55. }
  56. }
Success #stdin #stdout 0.14s 54508KB
stdin
5
2
5 6 8 9 10
stdout
2