Guest User

Jumping Caterpillars

a guest
Mar 15th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. import static java.lang.Integer.max;
  7. import static java.lang.Integer.min;
  8. import static java.lang.Integer.parseInt;
  9.  
  10. /**
  11.  * Created by bugkiller on 15/03/19.
  12.  */
  13. class JumpingCaterpillars {
  14.  
  15.     private static final int a[] = new int[1000000];
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  19.         int t, n, k;
  20.         String s[];
  21.         t = parseInt(br.readLine());
  22.         while (t-- > 0) {
  23.             s = br.readLine().split("\\s");
  24.             n = parseInt(s[0]);
  25.             k = parseInt(s[1]);
  26.             s = br.readLine().split("\\s");
  27.             for (int i = 0; i < k; i++) {
  28.                 a[i] = parseInt(s[i]);
  29.             }
  30.             System.out.println(countUnEaten(n, k));
  31.         }
  32.     }
  33.  
  34.     private static int countUnEaten(int n, int k) {
  35.         boolean sieve[] = new boolean[n + 1];
  36.         int count = 0;
  37.         initSieve(sieve, n, k);
  38.         for (int i = 1; i <= n; i++) {
  39.             if (!sieve[i]) {
  40.                 count++;
  41.             }
  42.         }
  43.         return count;
  44.     }
  45.  
  46.     private static void initSieve(boolean[] sieve, int n, int k) {
  47.         for (int i = 0; i < k; i++) {
  48.             if (a[i] <= n && !sieve[a[i]]) {
  49.                 for (int j = 1; a[i] * j <= n; j++) {
  50.                     sieve[a[i] * j] = true;
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
Add Comment
Please, Sign In to add comment