Advertisement
Spidey2182

Splitting Bill O(1) per case with Precomputation (Java)

Jul 5th, 2023 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class TestClass {
  4.     static int[] sieveOfEratosthenes(int n) {
  5.         int[] sieve = new int[n + 5];
  6.         Arrays.fill(sieve, 1);
  7.         sieve[0] = 0;
  8.         for (int i = 2; i * i < sieve.length; i++)
  9.             for (int j = i * i; j < sieve.length; j += i)
  10.                 sieve[j] = i;
  11.         return sieve;
  12.     }
  13.  
  14.     public static void main(String args[] ) throws Exception {
  15.         int[] sieve = sieveOfEratosthenes(1000000);
  16.  
  17.         Scanner sc = new Scanner(System.in);
  18.        
  19.         int testCases = sc.nextInt();
  20.         for (int testCase = 1; testCase <= testCases; testCase++) {
  21.             int n = sc.nextInt();
  22.             System.out.println(n / sieve[n] - sieve[n]);
  23.         }
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement